Weird result when using unique in a .m function file

2 次查看(过去 30 天)
I can't seem to figure out what is going on here. I'm trying to git rid of duplicate rows by using unique. At first I made a separate .m file called dup_remove.m that contained the following code
%function [dupRowValues]=dup_remove(p)
p = [-24.7697910000000,-15.8191235000000;-20.6771670000000,-3.54125200000000;-12.6771670000000,20.4587480000000;-20.6771670000000,-3.54125200000000;4.32283300000000,-1.04125200000000;-12.6771670000000,20.4587480000000;4.32283300000000,-1.04125200000000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000];
[up,I,J] = unique(p, 'rows', 'first');
ixDupRows = intersect(1:size(p,1), I);
dupRowValues = p(ixDupRows,:);
I get what I was expecting:
dupRowValues =
-24.7698 -15.8191
-20.6772 -3.5413
-12.6772 20.4587
4.3228 -1.0413
13.0197 -12.0402
-11.9803 -14.5402
So I figured that it should still work with the function excepting an input "p". So I uncommented the function, commented the matrix defining "p", and saved the file. I then went to my main program and defined p with the same values and called [dupRowValues]=dup_remove(p). Here is what i get:
dupRowValues =
-24.7698 -15.8191
-20.6772 -3.5413
-12.6772 20.4587
-20.6772 -3.5413
4.3228 -1.0413
4.3228 -1.0413
13.0197 -12.0402
-11.9803 -14.5402
-11.9803 -14.5402
How is this possible? For some reason the indexes in I and J are different. I also do not use up, I, or J anywhere else in the program
dup_remove.m
I =
1
2
3
9
5
8
J =
1
2
3
2
5
3
5
6
4
6
4
main function calling dup_remove.m with input p
I =
1
2
4
3
11
9
7
5
8
J =
1
2
4
3
8
4
7
9
6
9
5
Does anyone know what is going on? I do not see anything wrong with calling dup_remove.m.

回答(1 个)

Image Analyst
Image Analyst 2012-4-14
I get the same values no matter whether it's a script or a function. Here's code for your second case (my "test2.m"):
function test2
clc;
format compact;
workspace;
p = [-24.7697910000000,-15.8191235000000;-20.6771670000000,-3.54125200000000;-12.6771670000000,20.4587480000000;-20.6771670000000,-3.54125200000000;4.32283300000000,-1.04125200000000;-12.6771670000000,20.4587480000000;4.32283300000000,-1.04125200000000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000];
[dupRowValues] = dup_remove(p)
function [dupRowValues] = dup_remove(p)
% p = [-24.7697910000000,-15.8191235000000;-20.6771670000000,-3.54125200000000;-12.6771670000000,20.4587480000000;-20.6771670000000,-3.54125200000000;4.32283300000000,-1.04125200000000;-12.6771670000000,20.4587480000000;4.32283300000000,-1.04125200000000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000];
[up,I,J] = unique(p, 'rows', 'first');
ixDupRows = intersect(1:size(p,1), I);
dupRowValues = p(ixDupRows,:)
Like I said, the above code gives the same as if you hard coded in p and ran it as a script - no repeated rows like you showed.
If you're passing in p, I suspect that your numbers aren't equal out to the zillionth decimal place, and then perhaps you might be running into the issue of comparing floating point numbers like is discussed in the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
  8 个评论
Image Analyst
Image Analyst 2012-4-14
The dup_remove you uploaded is not the same as what you gave here. For example, there is no call to unique() or intersect. Moreover, it has this line in it:
if (L1 == L2) == true
which the FAQ specifically recommends against doing for floating point values. Recall, this is what it says:
% instead of a == b
% use:
areEssentiallyEqual = abs(a-b) < tol
% for some small value of tol relative to a and b
% perhaps defined using eps(a) and/or eps(b)
You are not doing that. Why not are you not heeding its warning and taking corrective action?
Harold
Harold 2012-4-14
That was an earlier attempt that I forgot to comment out. The comment out stuff under method 1 is what I posted. the main m-file to run is path_generation.m.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by