我希望能夠'圓'一個數字,如果它通過了一個閾值(而不是0.5),否則向下舍入。四捨五入數字與自定義閾值
這裏是我想出的一些蹩腳的代碼。在matlab中是否有內置函數,或者更優雅的解決方案(也許是矢量化的)?
function [ rounded_numbers ] = custom_round(input_numbers, threshold)
%CUSTOM_ROUND rounds between 0 and 1 with threshold threshold
[input_rows, input_cols] = size(input_numbers);
rounded_numbers = zeros(input_rows, input_cols);
for i = 1:length(input_numbers)
if input_numbers(i) > threshold
rounded_numbers(i) = 1;
else
rounded_numbers(i) = 0;
end
end
end
感謝
哇我是個白癡。謝謝 – waspinator 2012-04-26 22:47:50
@waspinator:總是樂於提供幫助:P – Jonas 2012-04-27 01:07:14