How can I multiply a vector by a scaling value with a loop?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Jan
am 18 Feb. 2022
Bearbeitet: Jan
am 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 Kommentare
Voss
am 18 Feb. 2022
Bearbeitet: Voss
am 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);
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!