How to make an if/else statement

5 ビュー (過去 30 日間)
Sandy
Sandy 2013 年 7 月 5 日
My data file consists of 3 columns and 100,000 rows. I want my code to go down a series of rows and test each row for the following.
if column1 > 13 & column1 < 15
put all those rows in a matrix (including columns 2 & 3)
elseif column1 > 2 & column1 < 4
put all these rows in a different matrix (including columns 2 & 3)
end
What's a good approach to this?

採用された回答

Guru
Guru 2013 年 7 月 5 日
FIND with the conditions is actually the way to do this.
% First read in your data file into MATLAB, let's say variable A
I1 = find(A(:,1) > 13 & A(:,1)<15);
I2 = find(A(:,1) > 2 & A(:,1) < 4);
% Now that you know the rows you want to assign, simply assign them...
B = A(I1,:);
C = A(I2,:);
HTH

その他の回答 (1 件)

Kelly Kearney
Kelly Kearney 2013 年 7 月 5 日
No if/else needed... just some logical indexing:
a = rand(100000,3)*15;
b = a(a(:,1)>13 & a(:,1)<15,:);
c = a(a(:,1)>2 & a(:,1)<4,:);

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by