How to extract a matrix of values from cell array of cell arrays of structs

8 ビュー (過去 30 日間)
Jerry Guern
Jerry Guern 2024 年 2 月 28 日
コメント済み: Jerry Guern 2024 年 3 月 5 日
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 件のコメント
Bruno Luong
Bruno Luong 2024 年 2 月 29 日
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
Jerry Guern 2024 年 2 月 29 日
編集済み: Jerry Guern 2024 年 3 月 4 日
@Bruno Luong Yes, it would indeed be easier if the data came already in form the I wanted it. Unfortunately, there's a module that outputs whole populated structs that I can't change. And we can't know in advance how large the 2D output will be in either dimension. So I'm stuck with the form it arrives in. But I have edited my question to say "...cell array of cell arrays..." Thanks for the clarification.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2024 年 3 月 1 日
Generate data (Thanks Voss)
n1 = 3;
n2 = 4;
mse = cell(1,n1);
for ii = 1:n1
mse{ii} = cell(1,n2); % row vector
for jj = 1:n2
mse{ii}{jj} = struct('metric',ii+(jj-1)*n1);
end
end
Single line code
reshape([cell2mat(cat(1,mse{:})).metric],length(mse),[])
ans = 3×4
1 4 7 10 2 5 8 11 3 6 9 12
  2 件のコメント
Bruno Luong
Bruno Luong 2024 年 3 月 1 日
移動済み: Bruno Luong 2024 年 3 月 1 日
I would first convert to struct array S
S = cell2mat(cat(1,mse{:}));
Then use S from now on and forget about mse.
metric_array = reshape([S.metric], size(S))
Jerry Guern
Jerry Guern 2024 年 3 月 5 日
Thank you! I especially appreciate the way you gave me what I actually wanted AND a more practical solution that I should have wanted.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 2 月 28 日
移動済み: Walter Roberson 2024 年 2 月 28 日
msea = zeros(n1,n2);
for i=1:n1
msea(i,1:n2) = [mse{i}{1:n2}.metric];
end
  4 件のコメント
Jerry Guern
Jerry Guern 2024 年 3 月 4 日
@Stephen23 Thanks. I literally have no reason to avoid loops beyond appeasing the "coding standards" monitors. Vectorizing does also make for tidier code in other languages, but this is not always so for matlab, like this case for example.
Jerry Guern
Jerry Guern 2024 年 3 月 5 日
If I wasn't required (against my wishes) to vectorize my code, I'd do is this way. This is succinct and highly readable code. The two best Answers I got on how fully vectorize this were respectively LONGER than this method and a single line of deep matlab code that I needed the manual to decipher. Left to my own devices THIS is how I'd code this up. Thanks.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by