Multiplying each column of a matrix with a specific value
30 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Areesh Adil
am 4 Feb. 2020
Kommentiert: Areesh Adil
am 4 Feb. 2020
I have a matrix
mat=[2,1,0,0;1,0,0,0;0,0,0,1]
I want to multiply the first column by 25, the second column by 5, the third column by 10 and the fourth column by 1 in a way that I get:
mat=[50,5,0,0;25,0,0,0;0,0,0,1]
Also, can the answer be generalized as I might end up adding more rows later on.
Thanks
0 Kommentare
Akzeptierte Antwort
James Tursa
am 4 Feb. 2020
Bearbeitet: James Tursa
am 4 Feb. 2020
mat=[2,1,0,0;1,0,0,0;0,0,0,1]; % 2D matrix
f = [25,5,10,1]; % row vector
result = f .* mat; % element-wise multiply with virtual expansion of row vector
If you have an earlier version of MATLAB, then use bsxfun:
result = bsxfun(@times,f,mat); % same functionality as f .* mat
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!