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 commentaires

KSSV
KSSV le 12 Juin 2020
Read about strcmp, strfind.
Hazar Can Dai
Hazar Can Dai le 12 Juin 2020
Thank you About that.I appriciate for your helps.
Anna Case
Anna Case le 12 Juin 2020
If you want case-insensitvity, use strcmpi()
Hazar Can Dai
Hazar Can Dai le 12 Juin 2020
I really appriciate for your helps.
dpb
dpb le 12 Juin 2020
Modifié(e) : dpb le 12 Juin 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...

Connectez-vous pour commenter.

 Réponse acceptée

dpb
dpb le 12 Juin 2020
Modifié(e) : dpb le 13 Juin 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.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by