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