How to find max of each iteration in cell array?
6 次查看(过去 30 天)
显示 更早的评论
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
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}))
采纳的回答
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]))
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 个评论
Star Strider
2023-1-17
As always, my pleasure! (Even though I had nothing to do with coding your 3D array!)
更多回答(2 个)
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 个评论
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
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 个评论
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!