Info

This question is closed. Reopen it to edit or answer.

I have a 9x1253 matrix: i found the max values of this matrix, but i need to find what row and column they belong to and show that.

1 view (last 30 days)
gasoline is a matfile of a 9x1253 matrix
this code is what i used to find the max values of the matrix
i started from 2 becuase the first row and column where names and not values.
gasoline
a=gasoline(2:end,2:end)
b=cell2mat(a)
c=max(b)
my question is how do i find the location of the max values in the original matrix gasoline in order to display the date for the column and row it occurs in.

Answers (1)

per isakson
per isakson on 3 Dec 2018
Edited: per isakson on 3 Dec 2018
See the subsection Largest Element in Matrix of
https://se.mathworks.com/help/matlab/ref/max.html
In response to comment:
This is close to the example in the documentation
%%
A = randn( 9, 1253 ); % sample data
%%
[ M, ix ] = max(A(:));
[ ixr, ixc ] = ind2sub( size(A), ix );
%%
M, ixr, ixc
returns
M =
3.5699
ixr =
5
ixc =
164
One has to follow the examples in the documention in detail. Similar isn't good enough.
  3 Comments
Matthew Weathersby
Matthew Weathersby on 3 Dec 2018
thank you so much for the response!
i was reading the question wrong. it wanted to max value of the max values.
I used this as my code:
a=gasoline(2:end,2:end)
b=cell2mat(a)
c=max(b)
d=max(max(b))
[row,col]=find(b==4.57)
gasoline{933+1}
So, i found that in row 8 column 933 is where the max value is, but i did not include row 1 column 1 because they are names not values.
i added one to the column number to take into consideration in the original array there is a column full of names to row 9.
this led me to believe the actual value was in column 933+1 for the column not included.
Thanks for the feed back
per isakson
per isakson on 3 Dec 2018
Edited: per isakson on 3 Dec 2018
The code based on the example of the documentation will give the same result and it's faster.
find(b==4.57) may give the wrong result because of floating point error. See https://se.mathworks.com/matlabcentral/answers/41877-matlab-floating-point-errors#answer_51565

Community Treasure Hunt

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

Start Hunting!