根据MATLAB中索引向量的值拆分矩阵

时间:2015-05-26 08:06:19

标签: matlab matrix

我有一个nx3矩阵,如下所示

1 x1 y1 
2 x2 y2 
2 x3 y3 
1 x4 y4 
   : 
   : 
1 (or2) xn yn 

这是来自两个通道的信号,但记录在同一矩阵中。 我想将矩阵分成两个矩阵

M1 = 

1 x1 y1 
1 x4 y4 
: 
: 
1 xi yi 

M2=

2 x2 y2 
2 x3 y3 
: 
: 
2 xj yj 

1 个答案:

答案 0 :(得分:1)

您可以使用逻辑indexig:

% generate some input data:
N = 10;
val = randi([1,2],[1,10]);
x = rand(1,N);
y = rand(1,N);
in = [val;x;y]';

% select all values where the value in the first column is 1
M1 = in(in(:,1)==1,:);
% select all values where the value in the first column is 2
M2 = in(in(:,1)==2,:);