How can I multiply a vector by a scaling value with a loop?
2 views (last 30 days)
Show older comments
Hi I have a vector A (1x200) and I want to multiply it for a scaling factor of 0.2 for 10 times. I would like to obtain a matrix B(10x200) where each row is the scaled vector?
thanks
0 Comments
Answers (1)
Jan
on 18 Feb 2022
Edited: Jan
on 18 Feb 2022
A = rand(1, 100);
S = 0.2 .^ (1:10).';
B = S .* A;
A smaler example:
A = rand(1, 4)
S = 0.2 .^ (1:3).'
B = S .* A
3 Comments
Voss
on 18 Feb 2022
Edited: Voss
on 18 Feb 2022
I believe that is what @Jan's answer does (using 5 elements here instead of 100 for ease of display):
A = rand(1, 5);
S = 0.2 .^ (1:10).';
B = S .* A;
format long
disp(A);
disp(B);
The first row of B is 0.2*A, and each successive row is 0.2 times the previous row.
If you want 10 copies of 0.2*A instead, you can say:
B = repmat(0.2*A,10,1);
disp(B);
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!