How to vectorize the following code?

1 回表示 (過去 30 日間)
pietro
pietro 2014 年 6 月 1 日
回答済み: dpb 2014 年 6 月 1 日
Hi all,
How can I vectorize the following code?
a=struct('cycles',[])
a(1).cycles=[rand(15,20];
a(2).cycles=[rand(15,20];
a(3).cycles=[rand(15,20];
for i=1:length(a)
totalnum(i)=sum(a(i).cycles)
end
Where totalnum should be a [3,20] array.
Thanks
cheers
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 1 日
What is a?
pietro
pietro 2014 年 6 月 1 日
You're on right, I have just edited the question. thanks

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

採用された回答

dpb
dpb 2014 年 6 月 1 日
reshape(sum([a.cycles]),length(a),[]).'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by