String comparison in cell arrays
110 次查看(过去 30 天)
显示 更早的评论
Hi, I need to match two cell arrays and need to get matched rows only remaining values i want to make it as empty cells. I am attaching the sample code here. Please let me know if any one knows
filt_data={'PID';'data';'new';'world'};
dat={'1';'2';'3';'4'};
Table1=[filt_data,dat]
ma={'PID';'new';'world'}
for i = 1:size(Table1)
a = Table1(i,1)
for j = 1:size(ma)
b = ma(j,1)
c = strcmp(a,b)
if c==1
Table1{i,1}=ma{j,1};
else
Table1{i,1}={};
end
end
end
I want the output as
Table1 ={'PID','1';'{}','{}';'new','3';'world','4'}
0 个评论
采纳的回答
Jan
2019-4-24
编辑:Jan
2019-4-24
filt_data = {'PID';'data';'new';'world'};
dat = {'1';'2';'3';'4'};
ma = {'PID';'new';'world'};
match = ismember(filt_data, ma);
Table1 = cell(numel(file_data), 2);
Table1(:) = {'{}'}; % Do you really what the char vector '{}'?!
Table(match, 1) = filt_data(match);
Table(match, 2) = dat(match);
Your text mentions "make it as empty cells", but in your example you use the char vector '{}'. I cannot guess, what you really want.
Alternatively:
Table1 = [filt_data(:), dat(:)];
match = ismember(filt_data, ma);
Table1(~match, :) = {'{}'}; % Or: {[]}, or {{}};
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!