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 Exchange에서 Matrix 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