Creating a column vector by multiplying the n-1 element by the corresponding element in another vector
Afficher commentaires plus anciens
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
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!
Andrei Bobrov
le 10 Oct 2013
out = [1;M(2:end).*F(1:end-1)]
Azzi Abdelmalek
le 10 Oct 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)
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!