Why does 'unique' not sort the rows?

2 views (last 30 days)
Akhil Vasvani
Akhil Vasvani on 3 Apr 2017
Commented: Akhil Vasvani on 4 Apr 2017
I'm trying to gather a set of data to be plotted. However, when I use unique, it does not sort the data and my plots are not correct.
function pos = Lube(m,n,len,filename)
[pos,nC,nT,lenC] = Graphic(m,n,lens,filename); %
lubepos=zeros(length(pos),2);%
for q=1:length(pos)
lubepos(q,:)=[nC(1)*pos(q,1)+nC(2)*pos(q,2),nT(1)*pos(q,1)+nT(2)*pos(q,2)];
end
radius=lenC/2/pi; %get the radius value
x=radius*cos(lubepos(:,1)/radius);
y=radius*sin(lubepos(:,1)/radius);
z=lubepos(:,2);
pos=[x,y,z]; %Create the positions matrix
pos=unique(pos,'rows');
fid=fopen(filename, 'wt'); %write to the filename
fprintf(fid, '%f %f %f \n',pos');%put in all the values
hEplot(filename)
end
The data I end up with is: -0.551329 0.000000 0.000000 -0.551329 0.000000 1.000000 -0.000000 -0.551329 1.500000 0.000000 0.551329 1.500000 0.551329 -0.000000 0.000000 0.551329 -0.000000 1.000000 0.551329 0.000000 0.000000 0.551329 0.000000 1.000000
As you can see, rows are repeated. How do I prevent this?
  2 Comments
Akhil Vasvani
Akhil Vasvani on 4 Apr 2017
Thank you @Stephen Cobeldick.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 3 Apr 2017
If you subtracted some of the rows that appear to be the same, you'll receive a very small but nonzero difference. If you want to use a little bit of a tolerance so two rows are considered the same if they're "close enough" to each other and you're using release R2015a or later, use uniquetol instead of unique.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!