How to find max of each iteration in cell array?

Hello!
i have A = 20x2 cell , in each cell i have 3 values of 3 iterations, i want to find the max of each iteration in 20 cell in order to get just two vectors
A1 [max1 max2 max3] A2[max1,max2,max3] after this i will find the max between A1 and A2
i don't know if cell2mat can support this knowing that i will use a large number of cell (100x100)
thanks in advance

4 Comments

Why use cells at all, if their contentsare all the same size?
Safia
Safia on 14 Jan 2023
Edited: Safia on 14 Jan 2023
@Matt J yes all have the same size, i used cell because i want to do a number of iteration in each element of two vectors each one contains 20 elements.
100 x 100 is not considered as "large" in times of giga bytes.
It is not clear, what "A1 [max1 max2 max3] A2[max1,max2,max3] " means. You want to find a maximum value in 20 cells, but you have 40.
Concatenating the cells and 1 or 2 calls of the max function should solve the problem:
B = cat(3, A{:}); % Maybe, or:
B = cat(4, cat(3, A{:, 1}), cat(3, A{:, 2}))
@Jan i want to find a maximum in each 20 cell and the first 20 cell is not related to the second 20cell that's why i want to extract each 3 maximum values of 3 iterations for each 20cell independently

Sign in to comment.

 Accepted Answer

It would help to have your cell array.
Try something like this —
M = randn(3, 20, 2);
C = squeeze(mat2cell(M,3,ones(20,1),[1 1]))
C = 20×2 cell array
{3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}
Cmax = cellfun(@max, C)
Cmax = 20×2
-0.0806 1.9797 1.7170 1.5291 0.0216 -0.1738 2.5621 0.6485 0.8975 0.3118 1.2401 0.9215 1.1480 2.3957 2.1077 0.0342 0.5151 1.0187 0.0069 2.3017
Check_1 = C{1,1} % Check Results
Check_1 = 3×1
-0.9548 -0.0884 -0.0806
Check_1_Max = max(Check_1)
Check_1_Max = -0.0806
Check_2 = C{1,2} % Check Results
Check_2 = 3×1
-0.6301 0.1064 1.9797
Check_2_Max = max(Check_2)
Check_2_Max = 1.9797
EDIT — (15 Jan 2023 at 19:05)
LD = load(websave('safia%20answer','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1264850/safia%20answer.mat'));
Result_10_element = LD.Result_10_element
Result_10_element = 51×2 cell array
{[92.4731 95.3678 91.0615]} {[91.0769 90.8078 91.8256]} {[93.4911 94.6023 92.6254]} {[94.8718 92.3944 93.5673]} {[94.6023 92.5620 87.9265]} {[93.6224 92.9178 92.6829]} {[93.8172 93.3934 92.2886]} {[91.3043 91.4508 92.5333]} {[92.0904 92.9412 92.4675]} {[91.7647 94.6176 91.5344]} {[93.5860 92.8191 90.5292]} {[92.1512 92.5065 92.5072]} {[94.3452 94.3820 92.0981]} {[94.1176 93.5103 93.7500]} {[93.0667 92.7649 92.6554]} {[91.5942 92.4324 92.4675]} {[93.2749 90.4762 89.1892]} {[93.6869 92.0635 93.1232]} {[90.0262 92.0330 94.1176]} {[94.6176 92.4157 88.8587]} {[92.3977 94.3978 94.2466]} {[93.3735 94.3452 91.0569]} {[ 89.5775 95 91.4835]} {[93.1232 95.2514 93.2961]} {[92.4791 91.6216 88.8587]} {[92.6829 92.1348 90.3047]} {[94.3020 89.8907 91.8495]} {[91.5301 92.0732 93.1646]} {[89.4428 90.9091 90.9814]} {[90.2941 93.5574 93.9241]} {[93.1759 91.7847 92.9412]} {[86.3501 90.4632 91.8699]}
Result_10_elementMax = cellfun(@max, Result_10_element) % Desired Result: Cell Maxima
Result_10_elementMax = 51×2
95.3678 91.8256 94.6023 94.8718 94.6023 93.6224 93.8172 92.5333 92.9412 94.6176 93.5860 92.5072 94.3820 94.1176 93.0667 92.4675 93.2749 93.6869 94.1176 94.6176
MaxColResult_10_elementMax = max(Result_10_elementMax) % 'Global' Column Maxima
MaxColResult_10_elementMax = 1×2
95.3678 95.2514
MaxRowResult_10_elementMax = max(Result_10_elementMax,[],2) % 'Global' Row Maxima
MaxRowResult_10_elementMax = 51×1
95.3678 94.8718 94.6023 93.8172 94.6176 93.5860 94.3820 93.0667 93.6869 94.6176
One of these should be the desired final result (I hope).
.

16 Comments

@Star Strider when i mentioned that i want the max of each iteration , i mean that between 51 values just i will extract the maximum , from the 3 columns in each cell vector i extract the maximum.
the final result should be like this
{[maxiter1 maxiter2 maxiter3]} {[maxiter1 maxiter2 maxiter3]}
That calculation is straightforward —
LD = load(websave('safia%20answer','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1264850/safia%20answer.mat'));
Result_10_element = LD.Result_10_element
Result_10_element = 51×2 cell array
{[92.4731 95.3678 91.0615]} {[91.0769 90.8078 91.8256]} {[93.4911 94.6023 92.6254]} {[94.8718 92.3944 93.5673]} {[94.6023 92.5620 87.9265]} {[93.6224 92.9178 92.6829]} {[93.8172 93.3934 92.2886]} {[91.3043 91.4508 92.5333]} {[92.0904 92.9412 92.4675]} {[91.7647 94.6176 91.5344]} {[93.5860 92.8191 90.5292]} {[92.1512 92.5065 92.5072]} {[94.3452 94.3820 92.0981]} {[94.1176 93.5103 93.7500]} {[93.0667 92.7649 92.6554]} {[91.5942 92.4324 92.4675]} {[93.2749 90.4762 89.1892]} {[93.6869 92.0635 93.1232]} {[90.0262 92.0330 94.1176]} {[94.6176 92.4157 88.8587]} {[92.3977 94.3978 94.2466]} {[93.3735 94.3452 91.0569]} {[ 89.5775 95 91.4835]} {[93.1232 95.2514 93.2961]} {[92.4791 91.6216 88.8587]} {[92.6829 92.1348 90.3047]} {[94.3020 89.8907 91.8495]} {[91.5301 92.0732 93.1646]} {[89.4428 90.9091 90.9814]} {[90.2941 93.5574 93.9241]} {[93.1759 91.7847 92.9412]} {[86.3501 90.4632 91.8699]}
Result_10_elementMax = mat2cell([max(cell2mat(Result_10_element))], 1, [3 3])
Result_10_elementMax = 1×2 cell array
{[94.6023 95.3678 94.2466]} {[94.8718 95.2514 93.9241]}
This approach converts the cell arrays to a matrix, takes the maximum of each column (creating a (1x6) vector) then converts that into a (1x2) cell array.
.
@Star Strider Perfect! this is exactly what I need ,i really appreciate your effort, thank you very much!
As always, my pleasure!
Hi! @Star Strider i want to ask you if your provided code should work when i apply it for 51x51 cell and the number of iteration is 10? because i repeated the same line command
Result_10_elementMax = mat2cell([max(cell2mat(Result_10_element))], 1, [10 10])
also i added these two commands
Result_10_elementMean = cellfun(@mean, Result_10_elementMax); %to get the average in each cell
MaxPerformance = max (Result_10_elementMean) % max average between cell
i get this error in the first
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [1 102].
error to use cell2mat
could you please give me clarification?
Thanks in advance
It would help to have a representative sample of the cell array.
Try something like this:
Result_10_elementMax = mat2cell([max(cell2mat(Result_10_element))], 1, [51 51])
That will create a (1x2) cell array with 51 elements in each of the two cells.
If you want them apportioned differently, for example a (1x34) cell array of 3 elements each, try this:
Result_10_elementMax = mat2cell([max(cell2mat(Result_10_element))], 1, [repmat(3,1,34)])
The elements of the second argument must sum to 102 in this instance, so any vector that accomplishes that will work.
.
That will create a (1x2) cell array with 51 elements in each of the two cells.
i tried with 51x4 cell in each one 2 values(2 iterations) and when i write
Result_10_elementMax = mat2cell([max(cell2mat(Result_10_element))], 1, [4 4])
Result_10_elementMax = 1x2 cell , 4 values in each one.
is there any modification in this line command in order to get 1x4 cell , 2 values in each one, so i could apply for 51x51cell
I thought this was all solved. Anyway, @Star Strider he did attach a .mat file in one of his replies to my answer. I'm attaching it again here.
Yes the code works but when I changed the number of cell and the number of values in each iteration an error is displayed
@Safia — It must be a factor of 102, so there are only a limited number of combinations that will work.
Factors_Of_102 = factor(102)
Factors_Of_102 = 1×3
2 3 17
Size__1 = 102/4
Size__1 = 25.5000
Since that is not an integer, 4 will not work, however other options will —
Size_2 = 102/6
Size_2 = 17
The sizes must be multiples of the ‘Factors_Of_102’ to work here. They have to add to 102, so for example:
ColSizes = [ones(1,25)*4 2]
ColSizes = 1×26
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2
SumColSizes = sum(ColSizes)
SumColSizes = 102
ColSizes = [ones(1,51)*2]
ColSizes = 1×51
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
SumColSizes = sum(ColSizes)
SumColSizes = 102
will both work, however in the first of these, not all the cells will have the same number of columns. The last cell will have only 2 columns. In the second, all cells will have 2 columns per cell.
Any vector that sums to 102 will work.
@Image Analyst — Thank you. That’s where I got the original file I used earlier.
.
The problem is I want to find the average of values in each cell and when I get 51x2cell in each one 51 values ,I can't check the elements in each cell to find max and average, I don't know if you understand the concept or not.
@Star Strider i'm asking how can i change my matrix to 3D array , may it could be easier to solve ?
I understand the concept. What I do not understand is your cell array, or what you want to do with it because I do not have it to work with.
I cannot respond to the 3D array approach until I understand the problem you are having with the cell array you have.
@Safia Are you sure you mean a 3-D array and not a 3-column, 2-D array?
@Image Analyst @Star Strider i changed my matrix to 3D array and it works very well! i really thank you very much for all your support
As always, my pleasure! (Even though I had nothing to do with coding your 3D array!)

Sign in to comment.

More Answers (2)

The more natural thing would be to have A be a 20x2xN array to keep track of the N iterations. Then you could simply do,
max(A,[],[1,2])

8 Comments

@Matt J it doesn't work, this is A, in each cell i have 3 results of 3 iterations, all what i need is to find the max value in each iteration, it means i will check the first value from 1 to 51 (in this example) and extract the max value, the the second, and finally get only one vector contain 3 max values and the same for the 2 array
It will work, but not with A as a cell array. Explore my suggestion that you recreate A instead as a numeric array.
@Matt J i didn't understand your suggestion, what do you mean by changing A to 20x2xN
@Safia you keep forgetting to attach your data in a .mat file, probably because you did not read the Community Guidelines like the posting form advised you to do.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
We'll check back later for your attached file.
@Safia I don't know what process you are using to produce A, but when rewritten to my suggestion, it should look something like this:
numIterations=3;
A=nan(20,2,numIterations); %Preallocate
for i=1:numIterations
A(:,:,i)=rand(20,2);
end
maxima = max(A,[],[1,2]);
whos A maxima
Name Size Bytes Class Attributes A 20x2x3 960 double maxima 1x1x3 24 double
@Matt J but A is a an array of cell, as the picture attached above.
i used cell2mat for just one vector cell , it transformed to a matrix [51x3] after this i easly found the max in each vector of this matrix. But i didn't know how to use it if i have more than one vector cell.
for example in my case if i have A=51x2 cell and each cell has 3 values , i will use cell2mat to transform A into 2 matrix, but when i have 100x100 how can i solve this?
Matt J
Matt J on 14 Jan 2023
Edited: Matt J on 14 Jan 2023
but A is a an array of cell, as the picture attached above.
Yes, but my suggestion was that you DON'T DO THAT. Change your code so that the iteration results get stored in a numeric array, and not a cell array.
@Safia, did you expand the comments to show the hidden ones? Did you overlook my question where I asked you directly for you to attach your A in a .mat file?
save('safia answers.mat', 'A');
Are you just refusing to do so? Frankly I'm surprised for the volunteers to keep trying to help you when you are not making it easy for them to help you. Why are you not coooperating so we can finally get your problem solved? Do none of the two solutions posted so far work, or you don't understand them? Maybe I'll post a less cryptic, simple for loop based solution.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

Is this what you want? A simple for loop based solution that's easy to understand?
% Create cell array, A because Safia won't upload it.
for row = 1 : 51 % or 20 or 100 or whatever you want. Not sure which since you've said all 3 things.
A{row, 1} = randi(9, 3, 1); % Each cell has 3 "iterations" as he calls them.
A{row, 2} = randi(9, 3, 1);
end
% Now that we have "A", we can begin.
% Find max in each column of A for all rows of A
for row = 1 : size(A, 1)
A1(row) = max(A{row, 1}); % Get max for this row in column 1.
A2(row) = max(A{row, 2}); % Get max for this row in column 2.
end
If not, say why.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
If you've read this part of the FAQ, I think you could do it yourself:

2 Comments

Safia
Safia on 15 Jan 2023
Edited: Safia on 15 Jan 2023
@Image Analyst i'm sorry for the misunderstand,i think that you want to see the screenshot in a file.
Now i upload the cell array as you tell me to do, i really thank you for your help but your provided code doesn't generate the required result, you will see attached two cell arrays and in each one 3 values of 3 iterations, so i want to check in each cell array the max value in each iteration and i will pass to the second cell array, in order to get at final just two row vectors which illustrate the max of each iteration of cell array 1 and the max of each iteration of cell array 2.
No problem. Now you'll know for next time how to prepare your answers so that people can quickly answer them. Anyway, you accepted Star's answer so it sounds like he figured it out and solved it for you. Thanks for accepting his answer.

Sign in to comment.

Categories

Asked:

on 14 Jan 2023

Commented:

on 17 Jan 2023

Community Treasure Hunt

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

Start Hunting!