Effacer les filtres
Effacer les filtres

how to store data in a matrix

12 vues (au cours des 30 derniers jours)
Lei
Lei le 3 Août 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 commentaires
Oleg Komarov
Oleg Komarov le 3 Août 2012
For next time review how to markup your question to make it more readable:
Lei
Lei le 5 Août 2012
thx Oieg

Connectez-vous pour commenter.

Réponse acceptée

John Petersen
John Petersen le 3 Août 2012
Modifié(e) : John Petersen le 3 Août 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 commentaires
Lei
Lei le 4 Août 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 le 6 Août 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by