フィルターのクリア

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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by