Filter löschen
Filter löschen

Efficient way to multiply an cell matrix with a scaler?

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Guillaume
Guillaume am 24 Nov. 2018
Bearbeitet: Guillaume am 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 Kommentare
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by