フィルターのクリア

Initialising tabular data structure using struct()

2 ビュー (過去 30 日間)
George Smith
George Smith 2023 年 1 月 6 日
編集済み: Stephen23 2023 年 1 月 6 日
For this I'm using an example as below
Eg, A table of data structures, size NL,ML, which has three data types within, at, bt and pt which all have a size nL, mL
Such that all of the below are reasonable:
data(NL,ML).bt(nL,mL) = 5 ;
data(NL-5,ML-2).pt(nL-2,mL-1) = 1 ;
To pre-allocate a data structure like this, I know you need to use struct like below
data = struct(pt,zeros(nL,mL)) ;
However, this creates a data structure that doesn't have the (nL,mL) as above. My current workaround is within the for-loop writing something as below
for N = 1 : NL
for M = 1 : ML
data(N,M) = struct('at',zeros(nL,mL),'bt',zeros(nL,mL),'pt',zeros(nL,mL)) ;
end
end
However this clearly isn't pre-allocated, as it's adding a new column and row to the first table bit each iteration. How can you pre-allocate a table of data structures like this?

採用された回答

Stephen23
Stephen23 2023 年 1 月 6 日
編集済み: Stephen23 2023 年 1 月 6 日
A simple REPMAT does the job:
M = zeros(nL,mL);
data = repmat(struct('at',M, 'bt',M, 'pt',M), NL,ML);

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by