Reshape Nx1 struct with field of Mx1 elements to N*Mx1 vector

8 ビュー (過去 30 日間)
Sabyrzhan Tasbolatov
Sabyrzhan Tasbolatov 2022 年 5 月 22 日
コメント済み: Stephen23 2022 年 5 月 22 日
I have a struct A of [1000x1] size. There is a field "bar" which has [100x1] elements.
I want to have 1 variable, let's say B, which will be [1000*100x1] of "bar" elements.
How can I do it without iterating a struct and appending on some prepared zeroed vector? Can reshape do it?
  1 件のコメント
Stephen23
Stephen23 2022 年 5 月 22 日
"How can I do it without iterating a struct and appending on some prepared zeroed vector? Can reshape do it?!
No, but a comma-separated list can do it:

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

採用された回答

Bruno Luong
Bruno Luong 2022 年 5 月 22 日
編集済み: Bruno Luong 2022 年 5 月 22 日
Something like
B = cat(1,A.bar)

その他の回答 (1 件)

MJFcoNaN
MJFcoNaN 2022 年 5 月 22 日
This is an example:
bar=rand(100,1)
bar = 100×1
0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464
A=struct;
A.bar=bar;
% copy A.bar to all 1000 elements
A(2:1000,1)=A(1)
A = 1000×1 struct array with fields:
bar
% table is a good intermediate form
tbl=struct2table(A);
B=cell2mat(cellfun(@transpose, tbl.bar, 'UniformOutput', false))
B = 1000×100
0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013 0.4600 0.1687 0.4194 0.2287 0.7427 0.7976 0.8690 0.4868 0.9914 0.4464 0.1298 0.0337 0.5265 0.2551 0.7338 0.6966 0.2969 0.3447 0.0830 0.7853 0.9779 0.2714 0.3507 0.4244 0.3516 0.3386 0.8395 0.6153 0.2840 0.6013
  1 件のコメント
Sabyrzhan Tasbolatov
Sabyrzhan Tasbolatov 2022 年 5 月 22 日
Thanks, I'm going to use cat as it's faster
Elapsed time is 0.001430 seconds.
I've also tried your option, it takes:
Elapsed time is 0.020119 seconds.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by