How to write a code for an iteration?

1 visualización (últimos 30 días)
Victor Fletcher
Victor Fletcher el 14 de Dic. de 2018
Comentada: Victor Fletcher el 14 de Dic. de 2018
Hi there I would like to run an iterative code.
I would like to start with a matrix f0=[a,b,c,d]
Then I would like to compute the following operation
f1=[a+b,c+d,a-b,c-d]
I would like this to be done iteratively and so that the script uses the value before.
that is f100 uses the a,b,c and value from f99.
How would I do this?

Respuesta aceptada

Dennis
Dennis el 14 de Dic. de 2018
f=zeros(100,4);
f(1,:)=[1 5 10 15];
for i=2:100
f(i,:)=[f(i-1,1)+f(i-1,2),f(i-1,3)+f(i-1,4),f(i-1,1)-f(i-1,2),f(i-1,3)-f(i-1,4)];
end
  2 comentarios
Victor Fletcher
Victor Fletcher el 14 de Dic. de 2018
HI yes thats exactly what I was looking for.
What about if I wanted to plot a row from that resulting 100x4 matrix?
e.g. the values in the 37th row??
Victor Fletcher
Victor Fletcher el 14 de Dic. de 2018
LL=f(100,:);
%%Plot%%
figure(1)
plot(LL, 'bo-', 'LineWidth', 2);
Ahh something like this plots the values from the 100th column!!
Got itt thanks!!

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 14 de Dic. de 2018
Editada: Torsten el 14 de Dic. de 2018
fold = [1 5 10 15];
n= 100;
for i=2:n
f = [fold(1)+fold(2),fold(3)+fold(4),fold(1)-fold(2),fold(3)-fold(4)];
fold = f;
end
f

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by