MATLAB Answers

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

2 views (last 30 days)
Laszlo Sleisz
Laszlo Sleisz on 14 Jul 2021
Answered: Stephen on 15 Jul 2021
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?

Accepted Answer

Matt J
Matt J on 14 Jul 2021
Edited: Matt J on 14 Jul 2021
One way,
Result(:,5) = round(Result(:,5),2,'significant');
numDigits=strlength(extractAfter(string(Result(:,5)),'.'));
numDigits(isnan(numDigits))=0
  7 Comments
Laszlo Sleisz
Laszlo Sleisz on 15 Jul 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.

Sign in to comment.

More Answers (1)

Stephen
Stephen on 15 Jul 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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!