English 中文(简体)
matlab: 在矩阵列小数点后返回 2 位数
原标题:matlab: return 2 numbers after decimal points in a matrix column

对不起, 如果这是愚蠢的问题, 但我是新的 MATLAB 。 我有一个大矩阵, 包含 < code> float 数字, 我想更改矩阵以显示小数点后的两个数字 。

当我在命令窗口输入低于代码的代码时 :

 sprintf( %.2f ,  ObjectTrack3(5,6))

sok sok 输出为: ans = 3.40

然而,当我在我的代码中添加 printf (%.2f, objectTrack3(i,6)) 以只显示 ObjectTrack3 第6栏中所有项目的小数点之后的两个数字时, 它只会给我一个错误 。

我怎样才能做到这一点?

(代码用于将单元格阵列转换为矩阵)

with guide of somebody I just noticed that the actual number for one of the cells of matrix is 44.849998474121094 but I just see 44.8500 0.400000005960465 is shown 0.4000

为什么增加零?为什么没有显示0.4和44.85?

最佳回答

I think you were on the right track, you just had some problems with the syntax.
Assuming that ObjectTrack is a matrix, what you should do is the following:

sprintf( %.2f
 , ObjectTrack3(:, 6))

This selects the 6th column from ObjectTrack3 and sends it to the sprintf command. Note that sprintf operates on each of the elements of its input column vector, hence the so that every element is printed in a new line.

Edit: 此答案还假设您只想要用想要的精确度来打印列的 em>, 而不是更改列。 如果您想要修改列, 请考虑Meming s 回答 。

问题回答

根据@Peter的评论, 我想你想要的是:

ObjectTrack3 = 圆(OjectTrack3 * 100) / 100;

它应该足够直观,可以理解。 sprintf 是指创建字符串,而不是双倍。





相关问题
PHP and unit testing assertions with decimals

I have a method that returns a float like 1.234567890.I want to test that it really does so. However, it seems that this returned float has different precision on different platforms so how do I ...

negative precision values in ostream

This is more of a question of curiosity but does anyone know how negative precision values are handled in C++? For example: double pi = 3.14159265; cout.precision(-10); cout.setf(ios::fixed, ios::...

Set the display precision of a float in Ruby

Is it possible to set the display precision of a float in Ruby? Something like: z = 1/3 z.to_s #=> 0.33333333333333 z.to_s(3) #=> 0.333 z.to_s(5) #=> 0.33333 Or do I have to override ...

Question with floating point addition

float totalAmount = 0; . . .//totalAmount assigned value 1.05 correctly . totalAmount += float.Parse(dataRow["Amt"].ToString()); //where dataRow["Amt"] has value 4.93 The answer I get for ...

Creating image or graphic in C# from decimal data?

Is it possible to create an image or graphic using vector data that has decimal components that vary? And, of course, if its possible... then how? For example: a vector has two points where point ...

Precision nightmare in Java and SQL Server

I ve been struggling with precision nightmare in Java and SQL Server up to the point when I don t know anymore. Personally, I understand the issue and the underlying reason for it, but explaining that ...

热门标签