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

2 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 10 Okt. 2013
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)]);

Weitere Antworten (3)

sixwwwwww
sixwwwwww am 10 Okt. 2013
Bearbeitet: sixwwwwww am 10 Okt. 2013
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!

Andrei Bobrov
Andrei Bobrov am 10 Okt. 2013
out = [1;M(2:end).*F(1:end-1)]

Azzi Abdelmalek
Azzi Abdelmalek am 10 Okt. 2013
F=[2;3;4;5;6]
n=numel(F)
M=zeros(n,1);
M(1)=1;
for k=2:n
M(k)=M(k-1)*F(k);
end
disp(M)

Community Treasure Hunt

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

Start Hunting!

Translated by