フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by