フィルターのクリア

Efficient way to multiply an cell matrix with a scaler?

1 回表示 (過去 30 日間)
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman 2018 年 11 月 24 日
コメント済み: Guillaume 2018 年 11 月 27 日
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

採用された回答

Guillaume
Guillaume 2018 年 11 月 24 日
編集済み: Guillaume 2018 年 11 月 25 日
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 件のコメント
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman 2018 年 11 月 26 日
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 2018 年 11 月 27 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by