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 Comments

Read about strcmp, strfind.
Thank you About that.I appriciate for your helps.
If you want case-insensitvity, use strcmpi()
I really appriciate for your helps.
dpb
dpb on 12 Jun 2020
Edited: dpb on 12 Jun 2020
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...

Sign in to comment.

 Accepted Answer

dpb
dpb on 12 Jun 2020
Edited: dpb on 13 Jun 2020
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.

More Answers (0)

Tags

Asked:

on 12 Jun 2020

Edited:

dpb
on 13 Jun 2020

Community Treasure Hunt

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

Start Hunting!