Effacer les filtres
Effacer les filtres

How to vectorize the following code?

3 vues (au cours des 30 derniers jours)
pietro
pietro le 1 Juin 2014
Réponse apportée : dpb le 1 Juin 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 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 1 Juin 2014
What is a?
pietro
pietro le 1 Juin 2014
You're on right, I have just edited the question. thanks

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 1 Juin 2014
reshape(sum([a.cycles]),length(a),[]).'

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by