向量元素是否在数组中出现

clc
clear all;
close all;

arrayA = [1,2,2,2;3,3,4,1];
arrayB = [3,2,5,1];
arrayB = sort(arrayB(:)); % 对数组arrayB进行列排序
arrayB(~[diff(arrayB);1]) = []; % 踢掉数组中重复元素;
[ra,rc] = size(arrayA);
m = zeros(ra,rc);
arrayA = arrayA(:);
lengthArrayA = length(arrayA);
[sa,ia] = sort(arrayA);
d = (~[diff(sa);0]);
if any(d)
ida = ia(d>0);
for i = ida'
m(i) = any(arrayA(i) == arrayB);
end
arrayA(ida) = NaN*ones(length(ida),1);
end
[x,ix] = sort([arrayA;arrayB]);
dx = [diff(x);1];
i = ix(dx == 0);
m(i) = ones(length(i),1);
m

posted on 2015-03-20 13:02  Kermit.Li  阅读(177)  评论(0编辑  收藏  举报

导航