-2

I want to get the index of a specific row in a 2-D array. I have a 15*15 array (Q), and I want to get index of the maximum number of 2nd row. I wrote this code, but an error occurred:

y=Q(2,:);
x=max(y)
??? Subscript indices must either be real positive integers or logicals.
Gyhth
  • 1,155
  • 9
  • 21
irisadaf
  • 101
  • 3
  • 12
  • 1
    You shouldn't get this error message from the code you provided. This is not where you are getting the error. Plus, you are getting the **value** and not the **index**. – Maroun Feb 27 '13 at 18:34
  • OK, But I don't know how to get index of it – irisadaf Feb 27 '13 at 18:51
  • possible duplicate of [matlab - argmax and argmin](http://stackoverflow.com/questions/14556733/matlab-argmax-and-argmin) – Shai Feb 27 '13 at 19:54
  • 3
    please check that you did not overwrite `max`: type `which max` and see if it returns a "built-in function" or a matrix/variable – Shai Feb 27 '13 at 19:55
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 16:07

1 Answers1

1

You are getting the maximum value of the second row, but you want the index of the maximum value. Here's how to get the index:

[~, index] = max(y)
shoelzer
  • 10,648
  • 2
  • 28
  • 49
  • the maximum numbers are multiple , but I want to get index of one of them – irisadaf Feb 27 '13 at 18:50
  • @irisadaf I think the error is not in the code shown. If `y` is a vector, the above code works just fine. – shoelzer Feb 27 '13 at 18:52
  • How to recognize this row is vector or not? this row (y) gets from Q array of double numbers – irisadaf Feb 27 '13 at 19:17
  • @irisadaf A vector is a 1xN, or Nx1 array. Are you sure that `y` is a vector? I think you have problems elsewhere in your code. – shoelzer Feb 27 '13 at 19:23
  • I think my MATLAB has some errors , because I wrote b=[1,2,3] and write max(b) , this error shown :??? Index exceeds matrix dimensions.!!!!!!!!! – irisadaf Feb 27 '13 at 19:33