像这样:
Product name number color
P1 x red
P1 x blue
p1 x blue
p2 x red
p3 x red
p4 x red
p4 x green
listItemToExclude = ['p1', 'p4']
我需要得到这个结果:
Product name number color
p2 x red
p3 x red
感谢您的帮助
答案 0 :(得分:2)
假设您的矩阵是一个单元格数组:
M = {'P1', 'x', 'red'; 'P1', 'x', 'blue'; 'p1', 'x', 'blue'; ...
'p2', 'x', 'red'; 'p3', 'x', 'red'; 'p4', 'x', 'red'; 'p4', 'x', 'green'};
您可以使用ismember
选择所需的行:
>> rowsOut = M(ismember(M(:,1),{'p2','p3'}),:)
rowsOut =
'p2' 'x' 'red'
'p3' 'x' 'red'
keyMatchFun = @(x) strcmpi(M(:,1),x);
keyMatchMasks = cellfun(keyMatchFun,{'p2','p3'},'uni',false);
rowsOut = M(any([keyMatchMasks{:}],2),:)
添加标题:
>> header = {'Product name','number','color'};
>> [header; outRows]
ans =
'Product name' 'number' 'color'
'p2' 'x' 'red'
'p3' 'x' 'red'
编辑:如果您从排除列表开始,例如listItemToExclude = {'p1', 'p4'};
,您可以通过以下方式获取列表(忽略大小写):
uniqueProducts = unique(lower(M(:,1)));
excludeMasks = cellfun(@(x)strcmpi(uniqueProducts,x),listItemToExclude,'uni',0);
listItemToInclude = uniqueProducts(~any([excludeMasks{:}],2))' %' use above
listItemToInclude =
'p2' 'p3'