Effacer les filtres
Effacer les filtres

Best practice of for loops and vectorisation, also maybe cumprod.

2 vues (au cours des 30 derniers jours)
Marshall
Marshall le 11 Sep 2014
Commenté : Marshall le 12 Sep 2014
Hello all,
Like many, I am entering Matlab from a C (embedded) background, so am struggling with the adjustment to vectors.
Simple cases such as the following are beginning to be understandable to me.
for i = 1:length(myVector)
dim(i)= i;
end
is equivalent to
dim = 1:length(myVector);
However I'm stuck on a slightly more complex case relating to nested arrays. I have a nested cell array (someArray) and I wish to know the cumulative product of all those elements.
someArray = {cell(1,4); cell(1,13); cell(1,3); cell(1,2); cell(1,5)}
someArray =
{1x4 cell}
{1x13 cell}
{1x3 cell}
{1x2 cell}
{1x5 cell}
The following works, but it feels clunky.
product = 1;
for i = 1:length(someArray)
dim(i) = length(someArray{i});
product = product*dim(i);
end
count =
1560
I feel like this is hacky and that there's a better way, but my thinking is stuck. Is there a way to use cumprod()? Any suggestions are welcome. I'm using ML ed. R2012b
  2 commentaires
Adam
Adam le 11 Sep 2014
You say you wish to know "the cumulative product of all those elements".
I assume that is just a slight mistake in the description since you seem happy with your result, but I was assuming you meant you want the product of all the elements in each cell array rather than simply the product of the lengths of each cell array. The former would be a little more complex, but if you do just want the product of the lengths then either your code or David Young's below will do fine.
Beware though that while cellfun looks neat it is, unfortunately rarely as performant as simply doing a for loop.
Marshall
Marshall le 11 Sep 2014
Thanks for the response. Yep, typo there. I just want the product.
The code here works to my satisfaction, I was just hoping there would be a leaner way to perform the same task and at the same time learn a new vectors method. I guess I'll leave it as it is.

Connectez-vous pour commenter.

Réponse acceptée

David Young
David Young le 11 Sep 2014
prod(cellfun(@length, someArray))
  14 commentaires
David Young
David Young le 11 Sep 2014
Marshall - yes, using {} rather than () is like doing an extra pointer dereference in C.
Marshall
Marshall le 12 Sep 2014
@Stephen, ""but more recent advice seems to be less concerned about them": implying therefore that "I don't have to bother learning it"?"
Not at all, I'm very interested in learning more about it! I was just interested in why there seems to have been a shift in advice over time when I've been reading forums.
Great advice and information from everyone here, thanks for the great welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays 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