how to store data in a matrix

59 visualizaciones (últimos 30 días)
Lei
Lei el 3 de Ag. de 2012
Hello everyone,
I have problem about how to store data in a matrix. Here is my code,aparently, theres problems.
a=5;
m=0;
M=[];
for i=1:72;
c1(i)=i;
c2(i)= c1(i)+1;
for j=2:5;
p1(i)=c1(i)+j;
if j==2
n=3:6; p2(i)=p1(i)+n;
elseif j==3
n=4:6; p2(i)=p1(i)+n;
elseif j==4
n=5:6; p2(i)=p1(i)+n;
elseif j==5
n=6; p2(i)=p1(i)+n;
end
if p2(i)<= 72
m=m+1;
M(m,:)=([m,c1(i),p1(i),p2(i),c2(i)]);
end
end
end
what i want to do here is:
c1=i,c2=c1+i,
if p1=c1+2, then p2=p1+3,p2=p1+4,p2=p1+5,p2=p1+6
if p1=c1+3, then p2=p1+4,p2=p1+5,p2=p1+6
if p1=c1+4, then p2=p1+5,p2=p1+6
if p1=c1+5, then p2=p1+6
and finally save data as [c1,c2,p1,p2] for each measurements
here is where i have problem:
if j==2
n=3:6;
p2(i)=p1(i)+n;
I dont know how could i achieve this, anyboday would like to give a hand? Thanks in advance
  2 comentarios
Oleg Komarov
Oleg Komarov el 3 de Ag. de 2012
For next time review how to markup your question to make it more readable:
Lei
Lei el 5 de Ag. de 2012
thx Oieg

Iniciar sesión para comentar.

Respuesta aceptada

John Petersen
John Petersen el 3 de Ag. de 2012
Editada: John Petersen el 3 de Ag. de 2012
Try this:
m=1;
for i=1:72;
for j=2:5;
p1 = i+j;
n=[j+1:6];
p2 = i + n;
for k=1:length(n);
M(m,:)=([m,i,i+1,p1,p2(k)]);
m=m+1;
end
end
end
  2 comentarios
Lei
Lei el 4 de Ag. de 2012
Hello, John, your code is very helpful!!! one last step to my dream code. I need to make sure I constrain that c1,c2,p1,p2 <=72. I went out this afternoon and just got back. So i will take a look at tomorrow and revise a little bit to achieve my final goal. Thanks for your guys help! i have learned a lot here!
John Petersen
John Petersen el 6 de Ag. de 2012
I'm assuming you just want to rid yourself of the rows with p2>72. You could do it by adding this to the end of the code.
ind = find(M(:,5)<=72);
M = M(ind,:);
or by inserting a test on p2 before the for k loop.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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