基于另一列的值获取该列的所有第一次出现-matlab

基于另一列的值获取该列的所有第一次出现-matlab,matlab,unique,Matlab,Unique,当m(:,2)==11时,我正在尝试获取所有第一次出现的m(:,1) 在我的示例中,我希望检索向量: m = repmat([0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2;11,11,22,22,33,33,11,11,22,22,33,33,11,11,22,22,33,33],1,2)' 我认为unique可能就是这个函数,但是unique(m(:,1),'rows')忽略重复的值 0 (from row 1, because it's the first o

m(:,2)==11时,我正在尝试获取所有第一次出现的
m(:,1)
在我的示例中,我希望检索向量:

m = repmat([0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2;11,11,22,22,33,33,11,11,22,22,33,33,11,11,22,22,33,33],1,2)'
我认为
unique
可能就是这个函数,但是
unique(m(:,1),'rows')
忽略重复的值

 0  (from row 1, because it's the first occurrence of (0,11))
 1  (from row 7, because it's the first occurrence of (1,11))
 2  (from row 13, because it's the first occurrence of (2,11))
 0  (from row 19, because it's the next first occurrence of (0,11))
 1  (from row 25, because it's the next first occurrence of (1,11))
 2  (from row 31, because it's the next first occurrence of (2,11))

下面是一种不使用
for
循环的方法:

ans =

     0
     1
     2

下面是一种不使用
for
循环的方法:

ans =

     0
     1
     2
还有一种方法:

a = find(m(:, 2) == 11); % index for all 11
repeat = [false; diff(a)==1]; % true if index diff is one
a(repeat) = []; % remove those indices for non-first occurrence
result = m(a, 1); % what you want
它也适用于以下向量

indx_11 = find(m(:,2) == 11);
% Find all the places where change occurs which will miss the 1st element
indxDiff = (diff(m(:,1))~=0); 
% Include the 1st element
m(intersect(find([1; indxDiff]), indx_11),1); 
还有一种方法:

a = find(m(:, 2) == 11); % index for all 11
repeat = [false; diff(a)==1]; % true if index diff is one
a(repeat) = []; % remove those indices for non-first occurrence
result = m(a, 1); % what you want
它也适用于以下向量

indx_11 = find(m(:,2) == 11);
% Find all the places where change occurs which will miss the 1st element
indxDiff = (diff(m(:,1))~=0); 
% Include the 1st element
m(intersect(find([1; indxDiff]), indx_11),1);