Avoid multiplying with 1 in element wise multiplication for large matrices
조회 수: 7 (최근 30일)
이전 댓글 표시
Hello, I have really large matrices (100000x100000) I want to multiply them element wise. One of them has %90 ones and I would like to avoid multiplying with those. Is there a quick way of doing that? That will save a lot of cpu time.
댓글 수: 0
답변 (2개)
Walter Roberson
2016년 3월 14일
Constructing the logical mask and extracting the subset and storing it back, would probably take more time than just having it proceed. However, if the positions of the non-1's is not changing, perhaps it would be worth constructing a sparse logical vector.
댓글 수: 0
Guillaume
2016년 3월 14일
I'm really not convinced you'll have any gain in speed. Identifying the ones and filtering both input matrix is probably going to take just as long as the multiplication.
%A: a matrix
%B: another matrix the same size as A, with lots of 1.
%C = A .* B
C = A; %default resut is multiplication by 1
isnotone = B ~= 1; %identify the 1s in B
C(isnotone) = A(isnotone) .* B(isnotone); %multiply only those elements for which B is not one.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!