How to convert a structure array into vector
古いコメントを表示
Hello.
I have a structure array (1 x 50000) with 10 fields. The elements in the fields are arrays (1 x 50). I want to convert this structure array into a scalar structure with same fields such that, its size is (50000*50 x 1).
5 件のコメント
Jos (10584)
2019 年 8 月 9 日
A very small example with, for instance, two elements in the structure array, two fields and very small arrays will help.
Shubham Gupta
2019 年 8 月 9 日
編集済み: Shubham Gupta
2019 年 8 月 9 日
What you have shown in image and what you mentioned in description is completely different. I am not sure how it is supposed to help ? Also, it's good for us to understand the problem better through some example, elaborating what you want to achieve.
SS
2019 年 8 月 9 日
Jos (10584)
2019 年 8 月 9 日
編集済み: Jos (10584)
2019 年 8 月 9 日
This is not a small example :-)
Provide 1x3 Main structure (the input) and expected out. That would help
採用された回答
その他の回答 (1 件)
Jos (10584)
2019 年 8 月 9 日
Why on earth store scalar values like that? Why not have a simple, highly efficient M-by-N matrix, rather than a cumbersome M-by-1 structure array, with N fields, each storing a single scalar value.
% a structure array with N = 2 fields. Each field holds overall and in total 7 elements
S(1).F1 = [1,2,3,4,5];
S(2).F1 = [10,20];
S(1).F2 = [2,4,6,8,10];
S(2).F2 = [150,200];
A = arrayfun(@(f) [S.(f{:})].', fieldnames(S), 'un', 0)
A = [A{:}] % a 7-by-2 matrix
3 件のコメント
SS
2019 年 8 月 9 日
Jos (10584)
2019 年 8 月 9 日
You should store values in a way that make sense to you. What is the relationship between the two fields of a particular element of the structure array (example: what do fel1 and fel2 of Main(k) have in common.) The same holds for the values inside a field. What do Main(J).fel1(K) and Main(J).fel2(K) have in common?
In your example, I (we?) assumed that there is a direct relations ship between the position in the output (either as matrix or a structure) and the position in the input. However, this does not seem to be the case.
Think about that and then describe what kind of output you need for further processing...
Jos (10584)
2019 年 8 月 9 日
編集済み: Jos (10584)
2019 年 8 月 9 日
One option is to keep the fields blank, or fille the empty spots in my output with NaNs.
One can use my function PADCAT to pad shorter vectors (being concatenated fields) with NaNs:
S(1).F1 = [1,2,3,4,5];
S(2).F1 = [10,20];
S(1).F2 = 2 ;
S(2).F2 = [150,200];
A = arrayfun(@(f) [S.(f{:})].', fieldnames(S), 'un', 0)
A = padcat(A{:}) % a 7-by-2 matrix
PADCAT can be found on the File Exchange:
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!