Effacer les filtres
Effacer les filtres

Adding up sizes of cell arrays

8 vues (au cours des 30 derniers jours)
Jonathan
Jonathan le 27 Juin 2012
Hi, I am wondering how to add up the sizes of cell arrays for each "class." The "classes" are part of a loop. I need to be able to index each sum into j.
for j = 1:NumberOfClasses
sizeOfCellArray{j} = size(CellArray{j})
sum(j) = sizeOfCellArray(j).*sizeOfCellArray(j+1)
end
I know that this is wrong, because sizeOfCellArray(j+1) gets an error (Index exceeds matrix dimensions), but am I on the right track?
  1 commentaire
Jan
Jan le 27 Juin 2012
Do not shadow the built-in function SUM by using a variable of the same name.

Connectez-vous pour commenter.

Réponse acceptée

Tom
Tom le 27 Juin 2012
You've made sizeofCellArray a cell, but then treat it like a double:
for j = 1:NumberOfClasses
sizeOfCellArray(j,:) = size(CellArray{j})
sum(j,:) = sizeOfCellArray(j,:).*sizeOfCellArray(j+1,:)
end
I'm not sure how you're accessing j+1 though? I would also advise that you change the name of the 'sum' variable, in case you want to use the built-in sum function later

Plus de réponses (1)

Kye Taylor
Kye Taylor le 27 Juin 2012
I assume you have a 1-by-NumberOfClasses cell array called CellArray.
Furthermore, I assume that each element of CellArray contains an array.
The following command will return a 1-by-NumberOfClasses double array, where the jth element gives the number of elements composing the array stored in CellArray{j}.
sizes = cellfun(@numel, CellArray);
The total number of elements contained in the contents of CellArray is given by
sum(sizes)
(Make sure you clear your variable sum - in your question's original code - before issuing the above command)

Catégories

En savoir plus sur Matrix Indexing 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