How to extract a matrix of values from cell array of cell arrays of structs
Show older comments
I have an array of array of structs, and I'd like to extract the value 'metric' from each, into a simple matrix. cell2mat and other functions just gave me various errors, so I wrote the following give-up code. It works, but is there a better way?
msea = zeros(n1,n2);
for i=1:n1
for j=1:n2
msea(i,j)=mse{i}{j}.metric;
end
end
3 Comments
Fangjun Jiang
on 28 Feb 2024
better to provide and show your test data
Bruno Luong
on 29 Feb 2024
Your data is not array of array of structs but cell array of cell array of structs. That's are not convenient to organize the data.
You might use for example 2D struct array if they have the same fields.
Jerry Guern
on 29 Feb 2024
Edited: Jerry Guern
on 4 Mar 2024
Accepted Answer
More Answers (1)
Walter Roberson
on 28 Feb 2024
Moved: Walter Roberson
on 28 Feb 2024
msea = zeros(n1,n2);
for i=1:n1
msea(i,1:n2) = [mse{i}{1:n2}.metric];
end
4 Comments
Jerry Guern
on 29 Feb 2024
"But I'm trying to be disciplined about using vectors instead of loops, so the MATLAB gurus won't chastise me"
This is a persistent myth about MATLAB, that loops should be avoided. Well written loops are neat and efficient.
Why specifically do you want to avoid loops?
Walter Roberson's answer will not work because the 2nd cell indexing produces a comma-separated list, into which further (dot) indexing is not possible. But if the data were in a structure array (as Bruno Luong recommended here), then something similar would be possible.
Jerry Guern
on 4 Mar 2024
Jerry Guern
on 5 Mar 2024
Categories
Find more on Data Type Identification 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!