필터 지우기
필터 지우기

how to store data in a matrix

조회 수: 12 (최근 30일)
Lei
Lei 2012년 8월 3일
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
Oleg Komarov
Oleg Komarov 2012년 8월 3일
For next time review how to markup your question to make it more readable:
Lei
Lei 2012년 8월 5일
thx Oieg

댓글을 달려면 로그인하십시오.

채택된 답변

John Petersen
John Petersen 2012년 8월 3일
편집: John Petersen 2012년 8월 3일
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
Lei
Lei 2012년 8월 4일
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 2012년 8월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by