how to do manipulation with decimal numbers in array

8 visualizaciones (últimos 30 días)
Steve Cleng
Steve Cleng el 3 de Abr. de 2022
Comentada: Steve Cleng el 3 de Abr. de 2022
I want to do the following manipulation with numbers in my arrays.
Let's assume we have
num = [0.23, 0.53, 0.04, 0.56];
Generate a number by taking most significant value on the decimal parts of each number in array one by one, then again do the same with the next decimal part. An get the number of 25053346
In other words, num = [0.a1a2, 0.b1b2, 0.c1c2, 0.d1d2]; and find a1b1c1d1a2b2c2d2
Thanks in advance,

Respuesta aceptada

Jan
Jan el 3 de Abr. de 2022
This cannot work reliably. Remember, that most decimal numbers cannot be represented exactly in binary format. Therefor some numbers, which are displayed as "0.23" might be stored as "0.22999999999999998" internally. So to do, what you are asking for, using the decimal places is a bad choice of input arguments.
But using strings or CHAR arrays works find, when you specify a number of rounded decimals.
num = [0.23, 0.53, 0.04, 0.56];
str = sprintf('%02.0f', num * 100)
str = '23530456'
str = [str(1:2:end), str(2:2:end)]
str = '25053346'
  1 comentario
Steve Cleng
Steve Cleng el 3 de Abr. de 2022
This is exactly what I needed. Thank you so much for your quick reply.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by