You might already know that to see if two variables are equal or not, we could use the == operator. But what about if you want to check two arrays or two structures are similar or not?
In such a case a simple == doesn't work. In such a case, the function isequal helps us to achieve this.
The usage is pretty simple, as you can see in the below examples...
Example 1:
A = zeros(2,2)+1e-10;
B = zeros(2,2);
check = isequal(A,B)
A and B differ by a small fraction, but still the variable check shows the value "0" after running the snippet.
Example 2:
A = zeros(2,2);
B = zeros(3,2);
check = isequal(A,B)
In the above case, both the arrays A and B have zero, but their size is different. Hence the answer, check shows "0".
Example 3:
A = struct('field1',0.05,'field2',200);
The structures are declared in the first two lines. You can see that, fundamentally both A and B are the same, even though the way they declared are a bit different. Guess what the value of "check" is after running the snippet? "1" !
Note that, isequal function does NOT treat NaN values as equal to each other. So make sure you take this fact into account when using isequal.
More information on this function here.
In such a case a simple == doesn't work. In such a case, the function isequal helps us to achieve this.
The usage is pretty simple, as you can see in the below examples...
Example 1:
A = zeros(2,2)+1e-10;
B = zeros(2,2);
check = isequal(A,B)
A and B differ by a small fraction, but still the variable check shows the value "0" after running the snippet.
Example 2:
A = zeros(2,2);
B = zeros(3,2);
check = isequal(A,B)
In the above case, both the arrays A and B have zero, but their size is different. Hence the answer, check shows "0".
Example 3:
A = struct('field1',0.05,'field2',200);
B = struct('field2',200,'field1',0.05);
check =
isequal(A,B)
Note that, isequal function does NOT treat NaN values as equal to each other. So make sure you take this fact into account when using isequal.
More information on this function here.
Comments
Post a Comment