How to use sortrows command in java.math.BigInteger?

조회 수: 2 (최근 30일)
Ammy
Ammy 2022년 10월 13일
답변: David Hill 2022년 10월 13일
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);

답변 (1개)

David Hill
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

카테고리

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