Clear Filters
Clear Filters

Multiplying each column of a matrix with a specific value

27 views (last 30 days)
I have a matrix
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:
Also, can the answer be generalized as I might end up adding more rows later on.

Accepted Answer

James Tursa
James Tursa on 4 Feb 2020
Edited: James Tursa on 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
  1 Comment
Areesh Adil
Areesh Adil on 4 Feb 2020
Thank you so much
I came up with a convoluted way where I would multiply with scalars and then horizontally concatenate. But this is so much simpler. Thanks

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!