strfind: how to set a cell for the pattern?
6 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I need to set a cell as pattern for strfind without using a for. Here an example
a={'1','2','3'};
b={'1','2'};
c=strfind(a,b)
c=[1 1 [] ];
thanks
cheers
5 件のコメント
Image Analyst
2014 年 7 月 11 日
What about this case:
a={'1','2','3'};
b={'2','1'};
Do you consider that the 1 and the 2 are found/matched, or not? They are not in the same locations , but both are in both arrays.
採用された回答
Image Analyst
2014 年 7 月 12 日
編集済み: Image Analyst
2014 年 7 月 12 日
Here is a simple, easy to understand way that will work:
clc; % Clear command window.
% Initialize variables.
a={'1','21','3'};
b={'2','1'};
% Initialize results.
% Let's use a simple numerical array rather than a cell array!
c = zeros(1, length(a));
% Scan each element of "a" for all the elements of "b".
for k = 1 : length(a)
for colb = 1 : length(b)
if ismember(b{colb}, a{k})
c(k) = 1;
break;
end
end
end
% Print out to command window.
c
0 件のコメント
その他の回答 (4 件)
Titus Edelhofer
2014 年 7 月 11 日
Hi,
not exactly the same result but similar:
ismember(a, b)
Titus
0 件のコメント
Chris E.
2014 年 7 月 11 日
Hello! Well I think this answers your question, it does not have a for loop, however it uses "ismember" rather then 'strfind', but I think the output is the same as what you want.
a={'1','2','3'}
b={'1','2'}
val = ismember(a,b)
val(val == 0)=[]
c = val
Hope that helps!
Jos (10584)
2014 年 7 月 11 日
Then please explain the relationship between a,b,and c. Why is c{2} equal to 1?
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!