How to sort rows?

2 次查看(过去 30 天)
Noor Fatima
Noor Fatima 2022-10-12
评论: David Hill 2022-10-13
>> A = [1 2; 4 3; 3 5; 2 1; 1 3; 4 5]
A =
1 2
4 3
3 5
2 1
1 3
4 5
>> B = sortrows(A)
B =
1 2
1 3
2 1
3 5
4 3
4 5
How to sort B, if entries of A are in java.math.BigInteger?
  1 个评论
Torsten
Torsten 2022-10-12
uint64 as a MATLAB data type is not sufficient for your purpose ?

请先登录,再进行评论。

采纳的回答

David Hill
David Hill 2022-10-12
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))<1
c=c+1;
else
temp=J(k+1);
J(k+1)=J(k);
J(k)=temp;
end
end
end
  3 个评论
Noor Fatima
Noor Fatima 2022-10-12
@David Hill May I request for the following
The above code sort perfectly fine w.r.t first coordinate.
But if first coordinate is the same, it is not sorting w.r.t second coordinate. Like it can be done with sortrows in the above-mentioned B.
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

请先登录,再进行评论。

更多回答(0 个)

类别

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