How to find max of each iteration in cell array?

조회 수: 9 (최근 30일)
Safia
Safia 2023년 1월 14일
댓글: Star Strider 2023년 1월 17일
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
Jan
Jan 2023년 1월 14일
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}))
Safia
Safia 2023년 1월 14일
@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

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

채택된 답변

Star Strider
Star Strider 2023년 1월 14일
편집: Star Strider 2023년 1월 15일
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
Safia
Safia 2023년 1월 17일
@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
Star Strider
Star Strider 2023년 1월 17일
As always, my pleasure! (Even though I had nothing to do with coding your 3D array!)

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

추가 답변 (2개)

Matt J
Matt J 2023년 1월 14일
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
Matt J
Matt J 2023년 1월 14일
편집: Matt J 2023년 1월 14일
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.
Image Analyst
Image Analyst 2023년 1월 15일
@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:

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


Image Analyst
Image Analyst 2023년 1월 15일
편집: Image Analyst 2023년 1월 15일
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
Safia
Safia 2023년 1월 15일
편집: Safia 2023년 1월 15일
@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.
Image Analyst
Image Analyst 2023년 1월 16일
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.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by