필터 지우기
필터 지우기

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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by