Rounding to user-defined number in matlab -


i have problem rounding user defined numbers.

i have mxn matrix different numbers between -3 , 12.

and have round each number in matrix nearst of [-3,0,2,4,7,10,12] have red rounding , floor function, can't figure out how round towards specific number.

use interp1 'nearest' option:

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'); 

this gives

result =     -3     2      7    10 

if prefer manually: compute pairwise absolute differences, find minimizing index each data value, , index matrix of allowed values:

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

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -