Add zero decimal digits in order to have all the elements of a matrix with same number of decimal digits

29 ビュー (過去 30 日間)
How can I add zero decimal digits to a number in order to have the same number of decimal digits in the numbers of a matrix? For instance, if I have only a "27" how can I change it to a "27.000"? I have already tried the function
compose("%.3f",A)
in order to have the same decimal formt of all the elements of matrix A, but it does not work.

採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 4 日
編集済み: Walter Roberson 2021 年 12 月 5 日
I already showed you how to use compose with a format to get a fixed number of digits.
  14 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 6 日
take the result of the standardizeMissing and array2table() with 'VariableNames' set to the header for the column. writetable() the results.
Walter Roberson
Walter Roberson 2021 年 12 月 6 日
https://www.mathworks.com/matlabcentral/answers/1601825-how-to-use-ismember-to-check-if-an-inputted-number-exists-in-a-matrix#comment_1868585 has more background about text files.

サインインしてコメントする。

その他の回答 (2 件)

Rik
Rik 2021 年 12 月 4 日
There is a distinction between the way data is stored and how it is displayed.
You can change the data type (double, single, cell, char, etc) to change the underlying data.
You can use functions like fprintf and sprintf to display your data a certain way.
  12 件のコメント
Rik
Rik 2021 年 12 月 5 日
Did you not see the if statement? The loop I showed will write only 1 value at a time, so you have full control over the format.
But the solution Walter showed you will also work.
Emilio Pulli
Emilio Pulli 2021 年 12 月 6 日
Thank you man! I use the Walter answer because was faster, but also your advices helped me understanding better the problem! Thank you again for the patience!

サインインしてコメントする。


G A
G A 2021 年 12 月 4 日
A = magic(3)
A = 3×3
8 1 6 3 5 7 4 9 2
B = num2str(A,'%.3f\t')
B = 3×17 char array
'8.000→1.000→6.000' '3.000→5.000→7.000' '4.000→9.000→2.000'

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by