Round the elements column of the matrix to the same digit than it is in the other column

5 visualizzazioni (ultimi 30 giorni)
I round elements one of column of matrix to 2 significant digits:
Result(:,5) = round(Result(:,5),2,'significant');
I wold like to round the elements another column of the same matrix to exactly same digits than it is in 'significant' digit column.
Is there any way to read back the number of digits from the 'significant' digit column to use it in other column?

Risposta accettata

Matt J
Matt J il 14 Lug 2021
Modificato: Matt J il 14 Lug 2021
One way,
Result(:,5) = round(Result(:,5),2,'significant');
numDigits=strlength(extractAfter(string(Result(:,5)),'.'));
numDigits(isnan(numDigits))=0
  7 Commenti
Matt J
Matt J il 14 Lug 2021
Thank you, now it works.
You are welcome, but please Accept-click the answer to indicate so.
Is it possible to format the number to N digits to the right of the decimal point, delete the unnecessary zeros.
Only if the array is converted to a string. If you plan to work with Result as numeric data, the only control you have over how it is displayed is through the format() command.
Laszlo Sleisz
Laszlo Sleisz il 15 Lug 2021
I think in this case I should use Cell array, because as I know not possible to create one matrix with different data type.

Accedi per commentare.

Più risposte (1)

Stephen23
Stephen23 il 15 Lug 2021
S = 2;
A = 32*rand(5,7)-13
A = 5×7
-8.9001 -12.7270 9.5712 14.7717 6.2782 3.5775 10.7267 16.4892 2.1012 12.5711 0.2720 8.0913 -11.5669 -9.3372 -5.9647 -12.7967 3.1765 5.9842 12.7080 15.4362 -4.9144 -8.0932 -12.6045 6.2981 14.2722 -9.1178 8.2538 -3.3988 15.7385 0.4380 2.9713 -6.0512 2.5839 -9.1609 -4.6357
round(A(:,5),S,'significant') % for comparison
ans = 5×1
6.3000 8.1000 13.0000 -9.1000 2.6000
P = S-1-floor(log10(abs(A(:,5))));
B = round(A.*10.^P)./(10.^P)
B = 5×7
-8.9000 -12.7000 9.6000 14.8000 6.3000 3.6000 10.7000 16.5000 2.1000 12.6000 0.3000 8.1000 -11.6000 -9.3000 -6.0000 -13.0000 3.0000 6.0000 13.0000 15.0000 -5.0000 -8.1000 -12.6000 6.3000 14.3000 -9.1000 8.3000 -3.4000 15.7000 0.4000 3.0000 -6.1000 2.6000 -9.2000 -4.6000

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by