MATLAB: Using find with a vector without having to use a for loop

findhistc

I have code which does
for t = 1: length(vector2)
idx(t) = find(vector <=vector2(t), 1, 'last');
end
Can I call find.m without using a for loop (or bsxfun.m) and if so how?
Or should I just use histc?
[~,idx]=histc(vector2, vector);

Best Answer

Hi,
use bsxfun and make sure one vector is a row vector and the other a column vector (a would be vector and b would be vector2)
a = [1 2 3 4 5 6]
b = [ 1 5 8 3 -10 2]
idx = bsxfun(@le,a',b)
Now you need to get a bit tricky I guess:
tmp = idx*(10.^[1:numel(b)]')
floor(log10(tmp))
Not sure if maybe log2 would be faster, you would need to try it:
tmp = idx*(2.^[1:numel(b)]')
floor(log2(tmp))