Если бы я собирался сделать это в R, я бы просто использовал функцию cut
.Я не могу найти эквивалент в MATLAB, но вот урезанная версия.(Не каламбур!)
function y = cut(x, breaks, right)
%CUT Divides the range of a vector into intervals.
%
% Y = CUT(X, BREAKS, RIGHT) divides X into intervals specified by
% BREAKS. Each interval is left open, right closed: (lo, hi].
%
% Y = CUT(X, BREAKS) divides X into intervals specified by
% BREAKS. Each interval is left closed, right open: [lo, hi).
%
% Examples:
%
% cut(1:10, [3 6 9])
% cut(1:10, [-Inf 3 6 9 Inf])
% cut(1:10, [-Inf 3 6 9 Inf], false)
%
% See also: The R function of the same name.
% $Author: rcotton $ $Date: 2011/04/13 15:14:40 $ $Revision: 0.1 $
if nargin < 3 || isempty(right)
right = true;
end
validateattributes(x, {'numeric'}, {});
y = NaN(size(x));
if right
leq = @gt;
ueq = @le;
else
leq = @ge;
ueq = @lt;
end
for i = 1:(length(breaks) - 1)
y(leq(x, breaks(i)) & ueq(x, breaks(i + 1))) = i;
end
end
Ваш вариант использования
cut(1:100, [-Inf 60 70 80 90 Inf], false)