X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 18 Sep 2015 08:43 AM by  Zachary Norman
Odd indexing in IDL
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

Zachary Norman



Basic Member


Posts:173
Basic Member


--
18 Sep 2015 08:43 AM
    I was wondering if someone might be able to explain what’s happening here. I am trying to create an array of values (A) and use a second array of values (B) to determine which values to read from A and return a vector (C) whose length is equal to that of B and has the values from A that correspond to the index values provided in B. Say I do the following: A = [0,1,2,3] B = [1,2,3,4,5,6,7,8,9] I’d expect: C = A[1,2,3,4,5,6,7,8,9] to return an out of dimension error – it does. But…If I instead do: C = A[B], I get: [1,2,3,3,3,3,3,3,3] Similarly, if I do: C = A[[1,2,3,4,5,6,7,8,9]], I again get: [1,2,3,3,3,3,3,3,3] and not the error that I would’ve expected to see. I’m obviously missing something here. I (wrongly?) thought the values in B would be used to index into A, so the value of 4 in B[3] should’ve been out of range for A. But it’s not. Any ideas what’s going on? I feel like I’m missing a subtlety in the syntax of what it means to nest square brackets. Alternatively, if there’s a better way to do this, I’m open to changing my approach.

    Zachary Norman



    Basic Member


    Posts:173
    Basic Member


    --
    18 Sep 2015 08:45 AM
    Hi, The behavior that you are seeing is actually because the two processes for getting array elements are fundamentally different. A[1,2,3,4,5] is not the same as A[[1,2,3,4,5]] and the second case is using an array as the array subscript. This behaves differently when the array has values that do not exist in the original array. I would take a look at the following docs page and read the section "Array Subscripts and Clipping" which explains what is going on. I hope that helps! http://www.exelisvis.com/...ays_as_Array_Su.html
    You are not authorized to post a reply.