How to find max of each iteration in cell array?
9 views (last 30 days)
Show older comments
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
Jan
on 14 Jan 2023
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}))
Accepted Answer
Star Strider
on 14 Jan 2023
Edited: Star Strider
on 15 Jan 2023
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]))
Cmax = cellfun(@max, C)
Check_1 = C{1,1} % Check Results
Check_1_Max = max(Check_1)
Check_2 = C{1,2} % Check Results
Check_2_Max = max(Check_2)
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_elementMax = cellfun(@max, Result_10_element) % Desired Result: Cell Maxima
MaxColResult_10_elementMax = max(Result_10_elementMax) % 'Global' Column Maxima
MaxRowResult_10_elementMax = max(Result_10_elementMax,[],2) % 'Global' Row Maxima
One of these should be the desired final result (I hope).
.
16 Comments
Star Strider
on 17 Jan 2023
As always, my pleasure! (Even though I had nothing to do with coding your 3D array!)
More Answers (2)
Matt J
on 14 Jan 2023
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
Image Analyst
on 15 Jan 2023
@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
on 15 Jan 2023
Edited: Image Analyst
on 15 Jan 2023
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
Image Analyst
on 16 Jan 2023
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!