Clear Filters
Clear Filters

how to store data in a matrix

12 views (last 30 days)
Lei
Lei on 3 Aug 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

Accepted Answer

John Petersen
John Petersen on 3 Aug 2012
Edited: John Petersen on 3 Aug 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 Comments
Lei
Lei on 4 Aug 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 on 6 Aug 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!