How to do a quick division between a column array and a matrix?

조회 수: 1 (최근 30일)
Benson Gou
Benson Gou 2019년 1월 28일
댓글: Benson Gou 2019년 1월 28일
Hello, All,
I want to divid a column array b (N x 1) by each column in a big sparse matrix A (N x M). My code is as follows:
dividCol = zeros(N,M);
for i = 1 : M
nonCol = find(abs(A(:,i))>0);
dividCol(nonCol,i) = b(nonCol)./A(nonCol,i);
end
But it is very slow. I do know if there exist a faster approach to write this code.
Thanks a lot in advance.
Benson

채택된 답변

Omer Yasin Birey
Omer Yasin Birey 2019년 1월 28일
편집: Omer Yasin Birey 2019년 1월 28일
Hi Benson, try this
% b = randi(25,25,1);
% A = randi(25,25,5);
[row,col] = find(abs(A(:,:))>0);
dividCol = bsxfun(@rdivide,b(row,1),A(row,col));
  댓글 수: 3
Omer Yasin Birey
Omer Yasin Birey 2019년 1월 28일
Yes I checked the code and it does every operation twice. I had to use loop and updated the code. Try it instead.
% b = randi(25,7,1);
% A = randi(25,7,14);
for i = 1:M
nonCol = find(abs(A(:,i))>0);
dividCol = bsxfun(@rdivide,b(nonCol),A(nonCol,i));
end
Benson Gou
Benson Gou 2019년 1월 28일
Omer,
Thanks for your prompt reply.
Is it possible to do this without using a loop? because loop normally takes more time.
Benson

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Denoising and Compression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by