How can I multiply a vector by a scaling value with a loop?

4 vues (au cours des 30 derniers jours)
Paola
Paola le 18 Fév 2022
Commenté : Jan le 18 Fév 2022
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

Réponses (1)

Jan
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)
A = 1×4
0.3654 0.1529 0.0723 0.4163
S = 0.2 .^ (1:3).'
S = 3×1
0.2000 0.0400 0.0080
B = S .* A
B = 3×4
0.0731 0.0306 0.0145 0.0833 0.0146 0.0061 0.0029 0.0167 0.0029 0.0012 0.0006 0.0033
  3 commentaires
Voss
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);
0.928773205480852 0.372047849369982 0.597515763533504 0.191352119560953 0.541467764860118
disp(B);
0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.037150928219234 0.014881913974799 0.023900630541340 0.007654084782438 0.021658710594405 0.007430185643847 0.002976382794960 0.004780126108268 0.001530816956488 0.004331742118881 0.001486037128769 0.000595276558992 0.000956025221654 0.000306163391298 0.000866348423776 0.000297207425754 0.000119055311798 0.000191205044331 0.000061232678260 0.000173269684755 0.000059441485151 0.000023811062360 0.000038241008866 0.000012246535652 0.000034653936951 0.000011888297030 0.000004762212472 0.000007648201773 0.000002449307130 0.000006930787390 0.000002377659406 0.000000952442494 0.000001529640355 0.000000489861426 0.000001386157478 0.000000475531881 0.000000190488499 0.000000305928071 0.000000097972285 0.000000277231496 0.000000095106376 0.000000038097700 0.000000061185614 0.000000019594457 0.000000055446299
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);
0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024 0.185754641096170 0.074409569873996 0.119503152706701 0.038270423912191 0.108293552972024
Jan
Jan le 18 Fév 2022
@Paola : I've inserted a small example. If this does not do, what you need, explain the difference. Give e.g. a small example of what you need instead.
What exactly does this mean: "multiply it for a scaling factor of 0.2 for 10 times"?

Connectez-vous pour commenter.

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!

Translated by