0

If I have the following array:

x = double([1, 1, 1, 10, 1, 1, 50, 1, 1, 1 ])

I want to do the following:

  1. Group the array into groups of 5 which will each be evaluated separately.

  2. Identify the MAX value each of the groups of the array

  3. Remove that MAX value and put it into another array.

  4. Finally, I want to print the updated array x without the MAX values, and the new array containing the MAX values.

How can I do this? I am new to IDL and have had no formal training in coding.

I understand that I can write the code to group and find the max values this way:

FOR i = 1, (n_elements(x)-4) do begin
  print, "MAX of array", MAX( MAX(x[i-1:1+3])
ENDFOR

However, how do I implement all of what I specified above? I know I have to create an empty array that will append the values found by the for loop, but I don't know how to do that.

Thanks

jenn
  • 11
  • 1
  • 5

2 Answers2

1

I changed your x to have unique elements to make sure I wasn't fooling myself. It this, the number of elements of x must be divisible by group_size:

x = double([1, 2, 3, 10, 4, 5, 50, 6, 7, 8])
group_size = 5
maxes = max(reform(x, group_size, n_elements(x) / group_size), ind, dimension=1)
all = bytarr(n_elements(x))
all[ind] = 1
x_without_maxes = x[where(all eq 0)]
print, maxes
print, x_without_maxes
mgalloy
  • 2,356
  • 1
  • 12
  • 10
0

Lists are good for this, because they allow you to pop out values at specific indices, rather than rewriting the whole array again. You might try something like the following. I've used a while loop here, rather than a for loop, because it makes it a little easier in this case.

x = List(1, 1, 1, 10, 1, 1, 50, 1, 1, 1)

maxValues = List()

pos = 4
while (pos le x.length) do begin
    maxValues.add, max(x[pos-4:pos].toArray(), iMax)
    x.Remove, iMax+pos-4
    pos += 5-1
endwhile

print, "Max Values : ", maxValues.toArray()
print, "Remaining Values : ", x.toArray()

This allows you to do what you want I think. At the end, you have a List object (which can easily be converted to an array) with the max values for each group of 5, and another containing the remaining values.

Also, please tag this as idl-programming-language rather than idl. They are two different tags.

spacemanjosh
  • 641
  • 1
  • 5
  • 14