我会使用矩阵B的列,使用第3列过滤矩阵A. 请注意,第3列的值以逗号分隔。
示例:P01存在于矩阵A的第3列,然后我们得到值A1。 其他示例:未返回A2,因为B中不存在P08或P09。
A = {'A1' 5 'P01,P02,P03,P04'; 'A2' 7 'P08,P09'; 'A3' 8 'P07'; 'A4' 8 'P10,P11'};
B = {'P01'; 'P07'; 'P10'; 'P11'}
A =
'A1' 5 'P01,P02,P03,P04'
'A2' 7 'P08,P09'
'A3' 8 'P07'
'A4' 8 'P10,P11'
B =
'P01'
'P07'
'P10'
'P11'
'P12'
'P13'
如何获得此结果?
C =
'A1'
'A3'
'A4'
提前感谢您的帮助。
答案 0 :(得分:2)
我认为这应该没问题。
A = {'A1', 5 'P01,P02,P03,P04';
'A2' 7, 'P08,P09';
'A3' 8, 'P07';
'A4' 8, 'P10,P11'};
B = {'P01'; 'P07'; 'P10'; 'P11'};
size(A)
C = {};
% for each row in A
for ai = 1:size(A, 1)
rowA = A(ai, :);
firstCol = rowA{1};
thirdCol = rowA{3};
% search if any element of B belongs to string in thirdCol
searchResult = cellfun(@(bv) ~isempty(findstr(bv, thirdCol)), B, ...
'UniformOutput', false);
% change results to matrix
searchResult = cell2mat(searchResult)';
% if any element of B in thirdCol then add firstCol to C
if any(searchResult) == 1
C{end + 1} = firstCol;
end
end
C
% gives
C =
'A1'
'A3'
'A4'
答案 1 :(得分:2)
aux = cellfun(@(c) A(~cellfun('isempty', strfind(A(:,3), c)), 1), B, 'UniformOutput', 0);
C = unique(vertcat(aux{:}));