How to find maximum value from different array in spesific dimension

2 views (last 30 days)
i have a variable array which consist of lets say 1x2 cell i want to find the maximum value for each column (combination of cell1 and cell2 value) example:
so, the maximum for column 1 is 1.7508 column 2 is 1.0713 column 3 is 5.8888
i tried using max(), but cant figure out how to find maximum value from different location(cell) please help..
thanks in advance

Answers (2)

Stalin Samuel
Stalin Samuel on 18 Jan 2016
s1{:} = rand(3)% cell 1
s2{:} = rand(3)% cell 2
data = [s1{:};s2{:}] %combining both cell data
Result = max(data)
  4 Comments
Stephen23
Stephen23 on 18 Jan 2016
Edited: Stephen23 on 18 Jan 2016
Yes, but it will be slow and buggy.
MATLAB is optimized to work fastest on numeric arrays, so whatever code you write will be slower than this solution.
We often get questions from beginners who have stuck all of their numeric data in cell arrays, and then want to perform some numeric operations on the total data. Not surprisingly the simplest, fastest and neatest solution is to take all of the numeric data out of the cell array and put it all in one numeric array. Performing the numeric operation is then usually quite trivial, like in this case. Because that is what numeric arrays are for.
Note on terminology: you write that "combining both into one cell", but actually the combined data are in a numeric array, not a cell array. Cell arrays have cells, numeric arrays have elements. Using the correct terminology makes it easier to communicate on this forum.
Guillaume
Guillaume on 18 Jan 2016
A slightly more generic way of concatenating the data:
c = {rand(50, 3); rand(50, 3)}; %demo data
m = vertcat(c{:});
However, as Stephen's said, if the matrices in the cell arrays are all the same size, it would be wiser to hold the data into a plain array to start with.

Sign in to comment.


Stephen23
Stephen23 on 18 Jan 2016
Edited: Stephen23 on 18 Jan 2016
This is the neatest solution, with the numeric arrays in one cell array:
C{1} = rand(3);
C{2} = rand(3);
max(vertcat(C{:}),[],1)

Tags

Community Treasure Hunt

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

Start Hunting!