如果超过阈值(不是 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
谢谢