如何利用 Matlab实现矩阵相同元素的查找

2025-03-11 06:45:07
推荐回答(1个)
回答1:

% 提取a中word
a=['an apple a day keeps the doctor away'];
a_space=find(abs(a)==32);
a_word=cell(length(a_space)+1,1);
a_word{1}=a(1:a_space(1)-1);
a_word{length(a_space)+1}=a(a_space(end)+1:end);
for i=1:length(a_space)-1
a_word{i+1}=a(a_space(i)+1:a_space(i+1)-1);
end
% 提取b中word
b=['i want to eat an apple'];
b_space=find(abs(b)==32);
b_word=cell(length(b_space)+1,1);
b_word{1}=b(1:b_space(1)-1);
b_word{length(b_space)+1}=b(b_space(end)+1:end);
for i=1:length(b_space)-1
b_word{i+1}=b(b_space(i)+1:b_space(i+1)-1);
end
% 比较a,b
k=1;result=[];
for i=1:length(a_word)
for j=1:length(b_word)
if strcmpi(a_word{i},b_word{j})==1
result{k}=a_word{i};
k=k+1;
break
end
end
end

N=length(result)
disp(result);

N =

2

'an' 'apple'