MATLAB Answers


Finding the maximum value of a matrix contained in a cell

Asked by Optical_Stress on 6 Apr 2017
Latest activity Commented on by Aditi Vedalankar on 11 Jun 2018
I have a 5-by-1 cell called C. I can extract the first matrix by C{1,1}{1,1};
When I say M=max((M{1,1}{1,1})) it returns a vector rather than a single integer. The matrix is a matrix of Intensities represented as uint8.
I believe it returns the maximum value of each column rather than the entire matrix. I can resolve this issue by finding the max of 'M' but was wondering if there was a single step method instead.
I had also tried M=max(cell2mat(M{1,1}{1,1})); but this did not work and i received error:
Cell contents reference from a non-cell array object.


Sign in to comment.

1 Answer

Answer by Guillaume
on 6 Apr 2017
 Accepted Answer

Note: avoid using subscript (2D) indexing on vectors. Prefer linear (1D) indexing. If C is a vector with 5 elements, C{5} will work whether or not it's a 5x1 or 5x1 cell array, whereas C{5,1} will error on a 1x5 cell array.
Note 2: "I have a 5-by-1 cell called C. I can extract the first matrix by C{1,1}{1,1};" The first element of C is C{1} (or C{1, 1} using subscript indexing). If you have to use C{1}{1} then the cells of your cell array are themselves cell arrays. That does not sound like it was intended from your description.
Anyway, If you want the max as a one liner:
M = max(reshape(C{1}{1}, 1, [])); %reshape the matrix into a vector before taking the max
However, I would recommend splitting the above in two for clarity
img = C{1}{1}; %get content of cell
M = max(img(:)); %get the max of all pixels


But if the cell is say 2 X 8 and every element of cell is a matrix, then how will we find the max of every matrix

Sign in to comment.