Filter löschen
Filter löschen

How to vectorize the following code?

3 Ansichten (letzte 30 Tage)
pietro
pietro am 1 Jun. 2014
Beantwortet: dpb am 1 Jun. 2014
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 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 1 Jun. 2014
What is a?
pietro
pietro am 1 Jun. 2014
You're on right, I have just edited the question. thanks

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 1 Jun. 2014
reshape(sum([a.cycles]),length(a),[]).'

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by