How to use sortrows command in java.math.BigInteger?
2 次查看(过去 30 天)
显示 更早的评论
Let A be BigInteger array (mx2)
I want to apply sortrows on A,
but it is not compatible with it .
import java.math.BigInteger.*;
e.g
A = [2 1; 2 2; 3 5; 3 6; 8 6; 8 4];
B = sortrows(A)
But when I used sortrows on BigInteger data, then the following error occur
Error using sort
Unknown command option.
Error in sortrows>sortBackToFront (line 119)
[~,ind] = sort(A(I,abs(ck)),directions{(ck < 0) + 1},opts{:});
Error in sortrows (line 73)
I = sortBackToFront(A, col, nanflag, compareflag);
0 个评论
回答(1 个)
David Hill
2022-10-13
import java.math.*
%load sample BigInteger Array (100x2)
for k=1:100
J(k,:)=[BigInteger(repmat(num2str(randi(1e14,1)),1,3)),BigInteger(repmat(num2str(randi(1e14,1)),1,3))];
end
%have to do sort manually, any sort algorithm will work but need to use
%compareTo() for BigIntegers
c=0;
while c~=size(J,1)-1
c=0;
for k=1:size(J,1)-1
if J(k,1).compareTo(J(k+1,1))==0&&J(k,2).compareTo(J(k+1,2))==1
temp=J(k+1);
J(k+1)=J(k);
J(k)=temp;
elseif J(k,1).compareTo(J(k+1,1))<1
c=c+1;
else
temp=J(k+1);
J(k+1)=J(k);
J(k)=temp;
end
end
end
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!