Why does 'unique' not sort the rows?
3 ビュー (過去 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!