MATLAB Answers

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

2 ビュー (過去 30 日間)
Laszlo Sleisz
Laszlo Sleisz 2021 年 7 月 14 日
回答済み: Stephen 2021 年 7 月 15 日
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 件のコメント
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 件)

Stephen
Stephen 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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by