How can I multiply a vector by a scaling value with a loop?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Jan
le 18 Fév 2022
Modifié(e) : Jan
le 18 Fév 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 commentaires
Voss
le 18 Fév 2022
Modifié(e) : Voss
le 18 Fév 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);
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!