필터 지우기
필터 지우기

Why is the function "isequal" giving wrong answer when comparing string fields of two structure arrays?

조회 수: 5 (최근 30일)
I have two structure arrays, say x and y. Each structure has two fields: name and data. Assume for example that:
x(1).name = 'name1'; x(2).name = 'name2';x(1).data = [1,2];x(2).data = [1,2];
y(1).name = 'name1'; y(2).name = 'name2';y(1).data = [1,2];y(2).data = [1,2];}
Now, the two structure arrays are identical. When I use "isequal" to compare x.data and y.data as following:
isequal(x.data,y.data) it gives logic 1 output.
But, when I use "isequal" to compare x.name and y.name, it gives logic 0 output, although they are identical. Is this a problem in the function "isequal"? And, what can I do to compare x.name and y.name correctly?
  댓글 수: 2
Stephen23
Stephen23 2018년 8월 5일
편집: Stephen23 2018년 8월 5일
"Now, the two structure arrays are identical. "
Sure.
"But, when I use "isequal" to compare x.name and y.name, it gives logic 0 output, although they are identical."
Sure. Because with your syntax you are not comparing those structures.
"Is this a problem in the function "isequal"?"
Nope, it is a problem with your code. The cause is very simple: this syntax
x.data
x.name
generates a comma-separated list, so they are directly equivalent to this:
x(1).data, x(2).data, ..., x(end).data
x(1).name, x(2).name, ..., x(end).name
which means your code is exactly equivalent to this:
isequal(x(1).data, x(2).data, ..., x(end).data, y(1).data, y(2).data, ..., y(end).data)
isequal(x(1).name, x(2).name, ..., x(end).name, y(1).name, y(2).name, ..., y(end).name)
which is thus equivalent to this (using your example data):
isequal([1,2], [1,2], [1,2], [1,2]) % all equal -> true
isequal('name1', 'name2', 'name1', 'name2') % all equal -> false
which according to the isequal help, " tf = isequal(A1,A2,...,An) returns logical 1 (true) if all the inputs are numerically equal." Are all of your multiple inputs numerically equal? In the first case they are, thus the true output, in the second case they are not, thus the false output. Thus there is no problem with isequal, it is doing exactly what it is documented to do.
To learn more about comma-separated lists:
"And, what can I do to compare x.name and y.name correctly?"
You can resolve this by putting the fields into one array, e.g. a cell array as jonas showed, or by simply comparing the complete structures.
Mohamed Abd El Raheem
Mohamed Abd El Raheem 2018년 8월 5일
@Stephen Cobeldick: Thanks a lot for your great explanation. I was indeed missing that stuff of comma separated lists, and also, like jonas, I was confused by that "isequal" gives true when it takes x.data and y.data. That result made me think it was a correct way to compare two fields of two structure arrays. Thanks again.

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

채택된 답변

jonas
jonas 2018년 8월 5일
편집: jonas 2018년 8월 5일
Can't tell why isequal returns false in your code. However, you can easily fix it by adding some curly brackets:
isequal({x.name},{y.name})
ans =
logical
1
  댓글 수: 3
Stephen23
Stephen23 2018년 8월 5일
"Can't tell why isequal returns false in your code."
Surely your answer {x.name} is a pretty big hint!
jonas
jonas 2018년 8월 5일
The fact that isequal(x.data,y.data) returned true made me confused. I failed to see that all four arrays were identical (thought there were two pairs).
Thanks for the explanation!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by