Round the elements column of the matrix to the same digit than it is in the other column
3 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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
Matt J
on 14 Jul 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.
More Answers (1)
Stephen23
on 15 Jul 2021
S = 2;
A = 32*rand(5,7)-13
round(A(:,5),S,'significant') % for comparison
P = S-1-floor(log10(abs(A(:,5))));
B = round(A.*10.^P)./(10.^P)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!