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?

采纳的回答

Matt J
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
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.
Laszlo Sleisz
Laszlo Sleisz 2021-7-15
I think in this case I should use Cell array, because as I know not possible to create one matrix with different data type.

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2021-7-15
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by