How can I found character in a 2D array

I have a problem about create a function which is tried to do at each row of a 2D character array there is a string which can have duplicate characters.I had tried to create function called "is_existing" that will accept two inputs one of the inputs arrays An array (say X) with above-given properties, and another 2D character array (say Y) whose number of columns is equal to the number of columns of X. The number of rows of these two arrays can be different. The output will be a column vector whose number of rows is equal to the number of rows of Y. For any row of Y, if there exists a row of X that is composed of a permutation of the string given in this row of Y, the corresponding element of the output vector will be logical 1. Otherwise, it will be logical 0.
----------------------------------------------------------------------------------------------------------------------------------------
I tried this
X=['133ABC36';'V2EL8A3V';'9MEHHET9';'919EY9EN'];
Y=['C3C3C3C3';'631A3B3C';'ETM99MEH'];
rowY=size(Y)(1);
colY=size(Y)(2);
rowX=size(X)(1);
colX=size(X)(2);
test=zeros(rowX,colX);
for i=1:rowY
for j=1:colY
for k=1:rowX
for t=1:colX
if Y(i,j)==X(k,t)
test(k,t)=1;
end
end
end
end
end
I couldn't create a function.It doesn't works like what I want.

5 件のコメント

KSSV
KSSV 2020 年 6 月 12 日
Read about strcmp, strfind.
Hazar Can Dai
Hazar Can Dai 2020 年 6 月 12 日
Thank you About that.I appriciate for your helps.
Anna Case
Anna Case 2020 年 6 月 12 日
If you want case-insensitvity, use strcmpi()
Hazar Can Dai
Hazar Can Dai 2020 年 6 月 12 日
I really appriciate for your helps.
dpb
dpb 2020 年 6 月 12 日
編集済み: dpb 2020 年 6 月 12 日
strcmp and friends will not find the permutations -- they only match on a position-by-position basis. I'm sure there's a regexp expression which would likely do it, but I'm not adept enough to be able to write it in a short amount of time...

サインインしてコメントする。

 採用された回答

dpb
dpb 2020 年 6 月 12 日
編集済み: dpb 2020 年 6 月 13 日

1 投票

The simplest way I can think to code would be
is_existing=ismember(sort(Y,2),sort(X,2),'rows');
or
is_existing=contains(string(sort(Y,2)),string(sort(X,2)));
It takes a looping over the two arrays in double loop to use ismember directly on the char() arrays to avoid the cross-comparison over the whole arrays that returns T for all strings in Y being in X as the characters do exist in the array somewhere. The sort puts in order so then a direct match is what are looking for, by the 'rows' option.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2020 年 6 月 12 日

編集済み:

dpb
2020 年 6 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by