2017-04-06 4 views
2

ユーザ定義番号への丸めに問題があります。 matlabのユーザ定義番号への丸め

I -3および12

そして私は丸め床機能に関するいくつかの赤色た[-3,0,2,4,7,10,12]のnearstに行列の各数値を丸めなければならないとの間の異なる数を有するM×Nのマトリックスを有するが、特定の番号に向かってどのように丸めるのか分かりません。

答えて

2

'nearest'オプションで使用interp1:あなたはそれを手動で行うことを好む場合はこれが

result = 
    -3  2 
    7 10 

与え

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

:、すべてのペアごとの絶対差を計算するごとに、最小化インデックスを見つけますデータ値、および許容値の行列へのインデックス:

[~, ind] = min(abs(bsxfun(@minus, x(:).',y(:))), [], 1); 
result = reshape(y(ind), size(x)); 
それはあなたが望むもののように聞こえる
1

discretize function次のとおりです。

>> % 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] 
関連する問題