Filter löschen
Filter löschen

saving multiplications with zeros in a loop

1 Ansicht (letzte 30 Tage)
D_coder
D_coder am 13 Sep. 2018
Kommentiert: D_coder am 14 Sep. 2018
%if true
for i iterations
Matrix = Matrix.*phase %Matrix has lot of zeros I want to skip multiplications with zero. Is it possible to do it in a single line without using if else or any loops
%end
Please dont use sparse, both matrix and phase are two-dimensional array

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Sep. 2018
Mask = Matrix ~= 0;
for i iterations
Matrix(mask) = Matrix(mask) * phase ;
end
This assumes that phase is a scalar nonzero value. It does not apply if phase is a vector or 2d array.
If phase is a 2d array then
phases = sparse(phase) ;
Matrix = sparse(Matrix) ;
for i iterations
Matrix = Matrix * phases;
end
Or better yet,
Matrix = sparse(Matrix) * (phase^iterations) ;
with no loop.
  4 Kommentare
D_coder
D_coder am 14 Sep. 2018
Bearbeitet: Walter Roberson am 14 Sep. 2018
actually this code is slow as compared to sparse. And sparse is slower as compared to the normal multiplication.Here is my detailed question. https://www.mathworks.com/matlabcentral/answers/418975-sparse-and-binary-mask
D_coder
D_coder am 14 Sep. 2018
Hey Walter my top priority is speed and saving multiplications.

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