2

I have a matrix like this one:

a= [ 61 65 63 ; ...
     21 21 24 ; ...
     34 2 51 ]
b= [ -1 0 8; ...
      -2 0 6; ...
      -4 0 2]
c=cat(3,a,b)

What I want to do is to find by rows of the first dimension on c the highest value, and then based on these values to get the values at the row and column but in the third dimension of c.

In my example, I am trying the following:

 [maxV,colIndx] =max(c,[],2)

 m=maxV(:,:,1)
 f=c(:,colIndx(:,1,1),2)

The vector m return half of my answer, ie

m =

    65
    24
    51

now, I want f to be like

f =

    0
    6
    2

However, my code doesn't return what I want. Any help is much appreciated!

Thanasis
  • 695
  • 1
  • 4
  • 17
  • Have read over [find](http://nl.mathworks.com/help/matlab/ref/find.html) and try to implement that. If it doesn't work edit your question with what you tried. – Johannes Mar 22 '16 at 15:13
  • I read the instruction for find but I don't think it helps in my problem as I already know the index, I just want to get the specific elements from the matrix. Thanks for the answer anyway. – Thanasis Mar 22 '16 at 15:24

1 Answers1

4

Try using linear indexing via the sub2ind function:

linIndx = sub2ind(size(c), (1:(size(c,1)))', colIndx(:,:,1), ones(size(c,1),1)*2);
f = c(linIndx)
Dan
  • 45,079
  • 17
  • 88
  • 157