Convert from a structure of arrays into an array of structures

バージョン 1.0.0.0 (1.49 KB) 作成者: Celso Reyes
converts from a structure, where each field contains an Nx1 array, into an 1xN array of structures
ダウンロード: 686
更新 2013/3/11

ライセンスの表示

Converts from a structure, where each field contains an Nx1 array, into an 1xN array of structures, where each field contains 1 value.

I decided to create and share this after not finding a readily available answer on the internet; though I suspect SOMEONE out there has already solved this issue.

USAGE:
S = structofarrays2arrayofstructs(A) assumes that A is a struct, with each field
containing Nx1 (columns) of values. (theoretically NxM values, where M may vary). This results in an Nx1 array of
structs, each containing 1 (or M) values.

Example
>> A.flower={'Daisy';'Rose';'Violet'};
>> A.color={'white';'red';'violet'};

>> S = structofarrays2arrayofstructs(A)

S =
1x3 struct array with fields:
flower
color

>> S(2)
ans =
flower: 'Rose'
color: 'red'

Note, Any cells it encounters are unwrapped.

In a test, with 6 fields, each with 377670x1 values (mixed, Cells of chars and arrays of Double), it took ~20 seconds to convert.

引用

Celso Reyes (2024). Convert from a structure of arrays into an array of structures (https://www.mathworks.com/matlabcentral/fileexchange/40712-convert-from-a-structure-of-arrays-into-an-array-of-structures), MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2011b
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
カテゴリ
Help Center および MATLAB AnswersData Types についてさらに検索
タグ タグを追加
謝辞

ヒントを与えたファイル: soa2aos(IN, LCHK)

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.0.0.0