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.
|