Azzera filtri
Azzera filtri

Efficient way to multiply an cell matrix with a scaler?

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Guillaume
Guillaume il 24 Nov 2018
Modificato: Guillaume il 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 Commenti
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman il 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 il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by