I wanna create vector of structures

Why
"for i=1:10 vec(i)=struct('a',i, 'b',20-i, 'c',i+3); end"
does not work?
How can I correct it to create vectors of structures?

1 件のコメント

Stephen23
Stephen23 2015 年 7 月 31 日
編集済み: Stephen23 2015 年 7 月 31 日
for k = k:-1:1
X(k).a = k;
X(k).b = 20-k;
X(k).c = k+3;
end

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 31 日
編集済み: Azzi Abdelmalek 2015 年 7 月 31 日

1 投票

ii=1:10
vec=struct('a',num2cell(ii),'b',num2cell(20-ii),'c',num2cell(ii+3))
Muthu Annamalai
Muthu Annamalai 2015 年 7 月 31 日

0 投票

You may also try, in addition to Azzi's code,
ii=1:10;
vec = arrayfun ( @(i) struct('a',i, 'b',20-i, 'c',i+3), ii );
depending on your stylistic preference.

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

タグ

質問済み:

2015 年 7 月 31 日

編集済み:

2015 年 7 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by