Effacer les filtres
Effacer les filtres

Efficient way to multiply an cell matrix with a scaler?

1 vue (au cours des 30 derniers jours)
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman le 24 Nov 2018
Commenté : Guillaume le 27 Nov 2018
Hi,
I have a for loop that multiplies a cell matrix with a scaler component. The size of K11{1,i} is never too big (maximum 50 by 50 matrix) and its a full matrix. What takes time is that sometimes the maximum value of "i" can go up to 10000 or more and I have to do it for many analysis. It would be very helpful if you could suggest a way to compute it faster by vectorizing the loop or any other way.
nonZeroDel = nnz(del);
delNonZero = nonzeros(del);
for i = 1:nonZeroDel
KE = KE + K11{1,i}*delNonZero(i);
end

Réponse acceptée

Guillaume
Guillaume le 24 Nov 2018
Modifié(e) : Guillaume le 25 Nov 2018
Since for your summation to succeed all the arrays in K11 must be the same size, convert that cell array to a 3D matrix. It will be a lot more efficient.
K11 = cat(3, K11{:});
delNonZero = permute(nonzeros(del), [3 2 1]); %move the non-zeros in the 3rd dimension
KE = sum(K11 .* delNonZero, 3);
  4 commentaires
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman le 26 Nov 2018
Much Thanks! It does give me the right answers, however, I am still struggling to have better results based on computational time. May be there is something in my code that I need to do in correct way.
Guillaume
Guillaume le 27 Nov 2018
Well, I don't know how you created that K11, but it shouldn't be a cell array in the first place. It should have been created as a 3D matrix from the beginning. Cell arrays are slower than matrices, often by a lot (as was your original code). In my answer, the slowest part will be the conversion from cell to matrix.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by