How to split a matrix by a column by whether values are greater or less than a number

7 ビュー (過去 30 日間)
Harry Stanton
Harry Stanton 2016 年 1 月 20 日
回答済み: Azzi Abdelmalek 2016 年 1 月 20 日
I have a matrix:
I would like to split this into two new matrices which are dependent on whether the values in column C are greater than 7 or less than 7.
I have tried a loop:
for i=1:12
if matrix(i,3)>7
A=(matrix(i,:))
elseif matrix(i,3)<7
B=(matrix(i,:))
end
end
this did not give me two matrices.
Can anyone help? Many thanks

採用された回答

Ilham Hardy
Ilham Hardy 2016 年 1 月 20 日
A = [5;3;6;1;7;8;2;3;4;8;6;5];
B = [45;25;36;87;45;14;2;31;59;6;47;2];
C = [4;5;8;6;11;14;9;5;1;19;3;10];
matt = [A,B,C];
matt1 = matt(find(matt(:,3)<7),:);
matt2 = matt(find(matt(:,3)>7),:);

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 1 月 20 日
A=randi(10,6,3)
idx=A(:,3)>7
A1=A(idx,:)
A2=A(~idx,:)

Community Treasure Hunt

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

Start Hunting!

Translated by