How to find max of each iteration in cell array?
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
Matt J
on 14 Jan 2023
Why use cells at all, if their contentsare all the same size?
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}))
Safia
on 14 Jan 2023
Accepted Answer
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
Safia
on 14 Jan 2023
Matt J
on 14 Jan 2023
It will work, but not with A as a cell array. Explore my suggestion that you recreate A instead as a numeric array.
Safia
on 14 Jan 2023
Image Analyst
on 14 Jan 2023
@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
Safia
on 14 Jan 2023
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.
Categories
Find more on Cell Arrays 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!