How to create a vector from a loop?

8 vues (au cours des 30 derniers jours)
Philippe Corner
Philippe Corner le 31 Août 2018
Réponse apportée : Paolo le 31 Août 2018
How could I save each iteration of the loop in a vector?
M=[-15.00 -5.00 5.00 15.00 19.93 0.1 4.86 -80.5 326.858 3092.000
-15.00 -5.00 15.00 25.00 23.61 0.0 7.33 32.0 96.830 3092.000
-15.00 -5.00 25.00 35.00 19.64 0.1 8.20 73.7 32.218 3092.000
-15.00 -5.00 35.00 45.00 18.43 2.1 7.43 -9.5 15.117 3092.000
-15.00 -5.00 45.00 55.00 13.72 4.9 7.16 125.9 6.429 3092.000
-15.00 -5.00 55.00 65.00 10.50 0.4 4.01 -82.8 3.076 3092.000
-15.00 -5.00 65.00 75.00 9.75 0.6 4.25 -18.5 1.904 3092.000
-15.00 -5.00 75.00 85.00 9.84 2.2 4.96 -70.1 1.345 3092.000
-15.00 -5.00 85.00 95.00 9.82 4.5 8.28 24.0 0.976 3092.000
-15.00 -5.00 95.00 105.00 11.09 3.5 -1.27 30.1 0.827 3092.000];
for i = 1:length(M)
v= (M(i,3))-((abs(M(i,2)))*i);
end
We should obtain a vector v like
v=[0
5
10
15
20
25
30
35
40
45];

Réponse acceptée

Paolo
Paolo le 31 Août 2018
Simply preallocate and then write to the column vector:
v = zeros(size(M,2),1);
for i = 1:size(M,2)
v(i,1)= (M(i,3))-((abs(M(i,2)))*i);
end

Plus de réponses (0)

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