필터 지우기
필터 지우기

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