Why does 'unique' not sort the rows?
조회 수: 4 (최근 30일)
이전 댓글 표시
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
2017년 4월 3일
편집: Stephen23
2017년 4월 3일
"As you can see, rows are repeated"
No they are not. Small differences in the floating-point values mean that those rows are actually different. Floating-point numbers have been explained a thousand times on this forum:
etc, etc, etc
Always use a tolerance when comparing floating point values.
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!