Round the elements column of the matrix to the same digit than it is in the other column
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Matt J
2021-7-14
编辑:Matt J
2021-7-14
One way,
Result(:,5) = round(Result(:,5),2,'significant');
numDigits=strlength(extractAfter(string(Result(:,5)),'.'));
numDigits(isnan(numDigits))=0
7 个评论
Matt J
2021-7-14
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.
更多回答(1 个)
Stephen23
2021-7-15
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!