strcmp help

조회 수: 14 (최근 30일)
Abra dog
Abra dog 2011년 11월 1일
How would i strcmp two character strings with different numbers of rows? Also how can i display what the differences are between the two rows?
  댓글 수: 3
Abra dog
Abra dog 2011년 11월 1일
these are cell arrays of strings
for example i have 30 objects on one string and 33 objects on the other string. I want to compare these two using strcmp and then show which ones didn't match
Fangjun Jiang
Fangjun Jiang 2011년 11월 1일
Then ismember(),intersect() or setdiff() may be useful.
ismember({'a','b'},{'a','c','ab'})
intersect({'a','b'},{'a','c','ab'})
setdiff({'a','b'},{'a','c','ab'})

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

채택된 답변

Sven
Sven 2011년 11월 1일
Like Fangjun said, ismember(),intersect() or setdiff() may be useful.
% Make a cell array of 10 short strings
firstSet = cellstr(char(randi(26,10,4)+96));
% And a second array using some of the first set mixed with other randoms
secondSet = cat(1, firstSet([2,7,9]), cellstr(char(randi(26,10,4)+96)));
% Randomise the order of this second set
secondSet = secondSet(randperm(length(secondSet)));
% Find matches
[firstMatch, firstMatchLocs] = ismember(firstSet,secondSet);
cell2print = cat(1, firstSet(firstMatch)', num2cell([find(firstMatch)'; firstMatchLocs(firstMatch)']));
fprintf('(%s), found at first index %d matches second index %d\n', cell2print{:})
fprintf('The following firstSet entries did not match the secondSet:\n')
cell2print = cat(1, firstSet(~firstMatch)', num2cell(find(~firstMatch)'));
fprintf('(%s) at index %d\n', cell2print{:})

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 1일
By definition, a character string only has one row.
If there is more than one row, you have a character array (or column vector.)
If you have two character arrays with the same width and the same number of rows, then
all(A==B,2)
will give you column vector of truth values of whether the rows match each other.
If you have two character arrays with different widths, then some would say that the two are never equal (because at the very least the number of trailing blanks would be different), and some would say that trailing blanks should be ignored (note: strcmp() explicitly includes trailing blanks!)
If you want to ignore trailing blanks and you have the same number of rows, then
cellfun(@isequal, cellstr(A), cellstr(B))
  댓글 수: 1
Abra dog
Abra dog 2011년 11월 1일
what if the width is the same but the number of rows don't match?

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

카테고리

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