Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在matlab中舍入到用户定义的数字_Matlab - Fatal编程技术网

在matlab中舍入到用户定义的数字

在matlab中舍入到用户定义的数字,matlab,Matlab,我对舍入到用户定义的数字有问题 我有一个MxN矩阵,不同的数字介于-3和12之间 然后我必须将矩阵中的每一个数字四舍五入到最接近的数值[-3,0,2,4,7,10,12]我有一些关于四舍五入和下限函数的红色信息,但无法计算如何向特定数字四舍五入。与最接近的选项一起使用: x = [-2.1, 1.5; 5.7, 10.8]; % data values y = [-3, 0, 2, 4, 7, 10, 12]; % allowed values result = interp1(y,y,x,'n

我对舍入到用户定义的数字有问题

我有一个MxN矩阵,不同的数字介于-3和12之间

然后我必须将矩阵中的每一个数字四舍五入到最接近的数值
[-3,0,2,4,7,10,12]
我有一些关于四舍五入和下限函数的红色信息,但无法计算如何向特定数字四舍五入。

最接近的选项一起使用:

x = [-2.1, 1.5; 5.7, 10.8]; % data values
y = [-3, 0, 2, 4, 7, 10, 12]; % allowed values
result = interp1(y,y,x,'nearest');
这给

result =
    -3     2
     7    10

如果您喜欢手动执行此操作:计算所有成对绝对差,找到每个数据值的最小化索引,并索引到允许值矩阵中:

[~, ind] = min(abs(bsxfun(@minus, x(:).',y(:))), [], 1);
result = reshape(y(ind), size(x));

听起来你想要的是:

>> % The set of values
>> setValues = [-3 0 2 4 7 10 12]';
>> % Find the midpoints between values to set as bin edges
>> binEdges = 0.5*(setValues(1:(end-1)) + setValues(2:end));
>> % Test a whole range of values
>> A = (-3.5:0.5:12.5)';
>> % Use discretize and include edges at -Inf and Inf to
>> % make sure we have the right # of bins
>> D = setValues(discretize(A, [-Inf; binEdges; Inf]));
>> % Compare the original values with the discretized version.
>> [A D]