Effacer les filtres
Effacer les filtres

How to sort rows?

2 vues (au cours des 30 derniers jours)
Noor Fatima
Noor Fatima le 12 Oct 2022
Commenté : David Hill le 13 Oct 2022
>> 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 commentaire
Torsten
Torsten le 12 Oct 2022
uint64 as a MATLAB data type is not sufficient for your purpose ?

Connectez-vous pour commenter.

Réponse acceptée

David Hill
David Hill le 12 Oct 2022
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 commentaires
Noor Fatima
Noor Fatima le 12 Oct 2022
@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 le 13 Oct 2022
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by