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)
Show older comments
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.
0 Comments
Answers (1)
per isakson
on 3 Dec 2018
Edited: per isakson
on 3 Dec 2018
See the subsection Largest Element in Matrix of
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
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!