Values of selected array indices

Imagine an array A of the form:
I would like to select certain cells of this matrix (shown in color) in a specific order, say, going from yellow, to blue to green to pink. The selection is stored in an array of indices which is: i=[1 1;2 4;4 1;5 4] according to our selection. Without using a loop over the indices, how can I now store the values associated with these indices in a new vector that reads as: V=[9 2 8 9] Which is sorted in the same order as the cell colors stated above.
Background: I have a contour map in the xyz form where x and y are the plane coordinates and z is the value of some variable on this xy plane (e.g. height). I use ginput(n) but as far as I know I only obtain the xy coordinates of the n selected points and not the value of z associated with that coordinate. So please feel free to comment on this as well!

 Accepted Answer

V=A(sub2ind(size(A),ix(:,1),ix(:,2)));
NB: I turned i into ix. I also use i in loop indices despite aliasing builtin function but I draw the line at arrays for other uses...

More Answers (1)

You can use sub2ind for this.
myIxs = arrayfun(@(rowIx,colIx) sub2ind(size(A),rowIx,colIx), i(:,1), i(:,2));
A(myIxs)

5 Comments

If you use arrayfun don't need sub2indx and conversely if use sub2indx don't need arrayfun
V=arrayfun(@(i,j) A(i,j),ix(:,1),ix(:,2));
See the above answer for sub2indx solution; it's already vectorized natively.
Thanks, that was great help! Now I want to proceed to create a vector that e.g. starts at the first point selected (yellow) and ends at the second point (blue) and covers all the elements on the route. It would be easy if the selection were always a diagonal matrix but in non-diagonal cases the line usually cuts through two adjacent elements of a row (or column). As stated in the original question this is about how I can click on two points of a contour plot, obtain the indices using ginput and the z values (which you kindly helped with) and then evaluate e.g. the maximum z value and the width of the area under the selected line (red line in picture below).
Brian Hannan
Brian Hannan on 11 Aug 2017
Edited: Brian Hannan on 11 Aug 2017
Yes that's right, dpb. arrayfun is unnecessary here. Thanks for pointing that out.
Brian Hannan
Brian Hannan on 11 Aug 2017
Edited: Brian Hannan on 12 Aug 2017
Saeid, there are a number of ways that you could find z(x,y) values on your surface from (x,y) values that lie on your line segment. A couple of ideas:
  • Find z(x,y) for all (x,y) pairs that are close (within some distance threshold) to the line segment.
  • Fit your data to a surface. Use the result to calculate z(x,y) for a set of points that you defined on your line segment.
  • Use interp2 (probably the best choice).
Altho I didn't time it; wonder how the two alternatives compare in efficiency...I hadn't thought of the arrayfun() route previously; does the transform of subscript conversion beat the overhead of the arrayfun internals for just simple assignment?

Sign in to comment.

Categories

Asked:

on 10 Aug 2017

Edited:

on 12 Aug 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!