2014-12-11 133 views

回答

2

这里有两种选择,或者寻找其中的B元件的A的元素,如果有A重复,或通过使用C矩阵可能失败匹配,查找第n之一。

A = [12 52 12 62 42]; 
C = logical([1 1 0 1 0]) 
B = A(C) 

[~,idx]=find(A==B(3)) %// finds where the third element of B was in A 
idx=find(cumsum(C)==3,1) %// finds where the third 1 in C is 
+0

加1,'cumsum'是该死的聪明。 – 2014-12-11 03:38:44

+0

此外,'A. * C'为您提供了一个带有“A”元素的向量,其中“C”为1,以及“C”为0的零点,这可能很有用。您也可以在代码中使用它,具体取决于您尝试实现的目标。 – David 2014-12-11 03:57:55

0

find给出了答案。假设indB表示B中的索引。然后A中的索引indA

indA = find(C); %// find nonzeros in C 
indA = indA(indB); %// take the indB-th nonzero in C