Find maximum of each cell array element efficiently

We have foe example a 3x5 cell array where each element is a matrix. Can we find the maximum of each cell element, i.e, a matrix, and store the corresponding value in a new 3x5 matrix? All this without for loops. Bellow there is the naive way.

Example:

a = rand(5,6);
b = rand(7,6);
c = rand(7,9);
d = rand(27,19);
CellArray = cell(2,2);
CellArray{1}=a;
CellArray{2}=b;
CellArray{3}=d;
CellArray{4}=c;

MaxResults = nan(size(CellArray));
for i=1:numel(size(CellArray))
    MaxResults(i) = max(max(CellArray{i})); 
end

Thank you.

Answers


Not guaranteed to be that much more efficient (until Matlab decides to multithread it), but you can use cellfun like this:

MaxResults = cellfun(@(x)max(x(:)), CellArray)

Need Your Help

Doctrine Query Builder ACOS function cannot be found

symfony doctrine-orm

I want to calculate distance in my custom repository class. The problem is that Doctrine throws exception while processing ACOS function:

Parallel between LMax Disruptor and Rx Framework concept?

java .net architecture concurrency reactive-programming

As I read here http://mechanitis.blogspot.fr/2011/06/dissecting-disruptor-how-do-i-read-from.html