Why does 'unique' not sort the rows?

3 ビュー (過去 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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by