How to find row and column of a value in cell array

I have this Cell Array ‘A’ of size 3 by 7
A = {
3 4 [] [] [] [] []
2 6 -2 2 -2.1 2 2
-5 -5 25 1 [] [] []}
I want the result of max value of this cell array i.e. ‘25’ at row 3 and column 3 shall be displayed.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 1월 25일
편집: Walter Roberson 2017년 1월 25일
B = A;
B(cellfun(@isempty, B)) = {NaN};
[maxval, maxidx] = max( reshape(cell2mat(B), [], 1) );
[maxrow, maxcol] = ind2sub(size(B), maxidx);
[maxval, maxrow, maxcol]
Note: this treats empty and NaN the same. If the array happened to consist entirely of NaN and empty locations, the code would return location 1 1, which might in some sense be incorrect because that location might be empty. However in such a case it is difficult to argue that NaN is a valid maximum since by definition NaN is Not A Number.

댓글 수: 5

its giving row and column as 1 and 1 which should have been row=3 and column = 3... that was my question....The maximum value and index as per your solution can be determined by this code
[maxval idx] = max([B{:}]);
Please tell me how to get the solution of row= 3 and column = 3
That solution worked fine for me...
Rememeber to assing
A={3 4 [] [] [] [] []; 2 6 -2 2 -2.1 2 2; -5 -5 25 1 [] [] []};
Vishal Sharma
Vishal Sharma 2017년 1월 25일
편집: Vishal Sharma 2017년 1월 25일
The index is '8'.... I want to get the solution in terms of rows (3rd) and column (3rd).... That's my main problem....
maxrow and maxcol give that information.
With your data
A = {
3 4 [] [] [] [] []
2 6 -2 2 -2.1 2 2
-5 -5 25 1 [] [] []}
then my code
B = A;
B(cellfun(@isempty, B)) = {NaN};
[maxval, maxidx] = max( reshape(cell2mat(B), [], 1) );
[maxrow, maxcol] = ind2sub(size(B), maxidx);
[maxval, maxrow, maxcol]
is going to create an answer
25 3 3
which is the maximum value then the row it is at and the column it is at.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2017년 1월 25일

댓글:

2017년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by