Comparing two string columns

조회 수: 6 (최근 30일)
Chirag Rathi
Chirag Rathi 2017년 12월 19일
댓글: Walter Roberson 2020년 4월 1일
I am trying to compare two string columns (of different dimensions) and I have typed in a code for that which is not working to my expectation. Any help would be highly appreciated!
So I have two string columns A (nx1) and B (mx1). After declaring these columns in my code,the conditions go like this:
for g = 1:length(A)
for h = 1:length(B)
if A(g) == B(h)
disp("%s %s\n",A(g),B(h));
end
end
end
When I ran this code, I got no matches. However, I know that there is at least 1 match (as I have seen the same strings in both the columns). So, any useful suggestions would be very helpful. Thank you in advance for your help!
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 12월 19일
편집: Walter Roberson 2017년 12월 19일
When you say "string", do you mean character vectors and cell array of character vectors, or do you mean the R2016b and later string object ?

댓글을 달려면 로그인하십시오.

답변 (2개)

Star Strider
Star Strider 2017년 12월 19일
I have only a vague idea of what you are doing. My guess is that you re comparing individual elements of both strings.
This is likely easier with the ismember function. Depending on the result you want, you can combine this with the any function if you want a single logical result at the end.
  댓글 수: 2
Chirag Rathi
Chirag Rathi 2017년 12월 19일
Yes, I am trying to compare the individual elements of both the string columns and thank you for the suggestion!
Star Strider
Star Strider 2017년 12월 19일
My pleasure!
If my Answer helped you solve your problem, please Accept it!

댓글을 달려면 로그인하십시오.


Bongane Radebe
Bongane Radebe 2020년 3월 31일
Please help I'm trying to use string comparison to compare elements of the 2 strings and actually store the common in a variable, how do i go about it?
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 4월 1일
C = intersect(A, B)
A and B can be string arrays or cell arrays of character vectors.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by