Creating a column vector by multiplying the n-1 element by the corresponding element in another vector

Hello,
I am quite new to matlab so I am hoping this is a simple question. I am trying to create a column Vector (M) where the first element is known (e.g. 1) and then subsequent elements are calculated by multiplying the above element in vector M (x-1) and the corresponding element in another vector (F)(y).
For example:
F=[2;3;4;5;6)
I want M to represent the following
M=[1;3;12;60;360]
I guess it would be something along the lines..
M=[1;M(1,1).*F(2,1);M(2,1).*F(3,1);M(3,1).*F(4,1);M(4,1).*F(5,1)
Am i on the right track?

 Réponse acceptée

You don't need to use a for-loop for this problem. Let the first known element of M be 'a'.
M = cumprod([a;F(2:end)]);

Plus de réponses (3)

Dear Hannah, here is the code as you require:
F = [2;3;4;5;6];
M = [1;3;12;60;360];
new_M = zeros(length(M),1);
for i = 1:length(M)
if i == 1
new_M(i) = M(i);
else
new_M(i) = M(i-1) * F(i);
end
end
Good luck!

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by