How can I multiply a vector by a scaling value with a loop?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Jan
2022-2-18
编辑:Jan
2022-2-18
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 个评论
Voss
2022-2-18
编辑:Voss
2022-2-18
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!