I need to sort an array but the contents are BigIntegers. When I try use the "sort(A)" function I get the following error:
Error using sort
Unknown command option.
Is it because the array is type BigInteger[] ? And if that's the case, how can I sort it?

 Respuesta aceptada

Jan
Jan el 29 de Abr. de 2022

0 votos

How dod you create the array? Please post a short example. I seems that you are talking about a Java class, not a Matlab array. Then use a Java method for sorting...
FEX: HPF might be useful. It has implemented an merge sort.

4 comentarios

Michelle Lear
Michelle Lear el 29 de Abr. de 2022
I have a for loop that uses a formula to generate the numbers. They were too late to be normal integers so I had to use java.math.* to use BigIntegers within matlab.
z_n = @(n) x_n(n).subtract(y_n(n)).mod(m1);
for i=1:1000
Z(i) = z_n(i);
end
Your code example does not contain any "java.math.BigInter". What is y_n and m1?
Please post a minimal working example, to reduce the need to guess, what you are doing.
With bold guessing:
a = java.util.ArrayList;
for k = 1:5
v = randi([0, 2^16]);
a.add(java.math.BigInteger(typecast(v, 'uint8')));
end
a
a = [542892608, 9623872, 12368960, 2154949440, 3235961408]
java.util.Collections.sort(a)
a
a = [9623872, 12368960, 542892608, 2154949440, 3235961408]
Michelle Lear
Michelle Lear el 30 de Abr. de 2022
I have tried what you wrote in your comment and I get the following :
No method
'java.util.Collections.sort' with
matching signature found.
I imported java.util.* but that doesn't change the outcome.
Jan
Jan el 2 de Mayo de 2022
@Michelle Lear: The link does not uniquely explain, how you have defined your array of BigIntegers. It would be helpful if you post your code instead of letting me guess the details.
I've showed you the code to inster the BigIntegers in a java.util.ArrayList, and this is a valid input for java.util.Collections.sort. If you use the suggested method, you will not get this error message.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2021b

Preguntada:

el 29 de Abr. de 2022

Comentada:

Jan
el 2 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by