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'}

采纳的回答

Jan
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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by