舍入数字

时间:2016-02-12 09:37:09

标签: matlab rounding

我正在努力使Matlab在一定的时间间隔内将数字舍入到数字。我有一个大的向量,我需要将它们向上或向下舍入。

% Input
A = [1 2 3 4 5 6 7 8 9 10]

% Interval of allowed numbers.
dE = 3;

% Rounding
B = round(A,dE); % Does not work like I desire.

% Desired output
B == [0 3 3 3 6 6 6 9 9 9 ]

2 个答案:

答案 0 :(得分:3)

% Input
A = [1 2 3 4 5 6 7 8 9 10];

% Interval of allowed numbers.
dE = 3;

% Rounding
B = round(A/dE)*dE; 

答案 1 :(得分:2)

您无法对整数进行舍入,因此需要将其除以所需的时间间隔dE。舍入后,您可以再次将其乘以dE

A = [1 2 3 4 5 6 7 8 9 10]
% Interval of allowed numbers.
dE = 3;    
B = dE * round(A / dE)
B =
     0     3     3     3     6     6     6     9     9     9