How to extract a matrix of values from cell array of cell arrays of structs
Ältere Kommentare anzeigen
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 Kommentare
Fangjun Jiang
am 28 Feb. 2024
better to provide and show your test data
Bruno Luong
am 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
am 29 Feb. 2024
Bearbeitet: Jerry Guern
am 4 Mär. 2024
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 28 Feb. 2024
Verschoben: Walter Roberson
am 28 Feb. 2024
msea = zeros(n1,n2);
for i=1:n1
msea(i,1:n2) = [mse{i}{1:n2}.metric];
end
4 Kommentare
Jerry Guern
am 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
am 4 Mär. 2024
Jerry Guern
am 5 Mär. 2024
Kategorien
Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!