Why does 'unique' not sort the rows?

조회 수: 4 (최근 30일)
Akhil Vasvani
Akhil Vasvani 2017년 4월 3일
댓글: Akhil Vasvani 2017년 4월 4일
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
Stephen23
Stephen23 2017년 4월 3일
편집: Stephen23 2017년 4월 3일
Akhil Vasvani
Akhil Vasvani 2017년 4월 4일
Thank you @Stephen Cobeldick.

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

답변 (1개)

Steven Lord
Steven Lord 2017년 4월 3일
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.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by