2

How can I implement this Matlab code without using a for loop?

b=10:10:50
a=50*rand(1,50);

for ii=2:numel(b)
    ind{ii}=find(a<b(ii) & a>b(ii-1));
end
3
  • 2
    What do you mean, 'vectorize' it? On each step of the loop you are throwing away the previously calculated value of ind, so you only need to perform the last step. Is there a mistake in your code? Mar 8, 2013 at 19:51
  • By vectorize I mean without the for loop. ind can be a cell for that matter. I'll edit the question so it'll be more clear.
    – user2041376
    Mar 8, 2013 at 21:41
  • To do exactly what you are doing here (using find, storing result in a cell array) a loop may well be your fastest bet. On the other hand, if your program can make use of logical indexing or some other technique, a non-loop solution might speed things up. You haven't provided enough information to know that, however.
    – tmpearce
    Mar 8, 2013 at 22:35

2 Answers 2

3

It looks like you are doing a histogram and keeping track of which element ends up in which bin. This means you can get "almost" what you want with the following lines:

a = 50 * rand(1, 50);
b = 10:10:50;
[h c] = histc(a, b);

Now c contains the index of the "bin" of each element in a. For example if

a = [15 22 9 7 25];

Then

c = [1 2 0 0 2];

Not sure of the value of collecting these into a cell array - it seems to me whatever you want to do with the values in ind can be done with c.

I suspect it may be hard to create a cell array (with possibly different lengths) with a "vector" operation (which implies things with the same length)... Would be interested to see someone produce a counterexample!

EDIT: I discovered my own counterexample... the following line produces a cell array ind just as your code did (the arrayfun command does have an implied for loop but is considered "vectorized").

ind = arrayfun(@(x)find(x==c),1:numel(b)-1, 'uniformoutput', false);

Note when this is done the cell array ind has values from cell ind{1} onwards, while your original code indexed from cell ind{2}. If that is an issue I'm sure you can fix it...

Also note that your code is generating random numbers between 0 and 50, but your "valid bins" are only between 10 and 50 (because of how you wrote your algorithm). Thus the sum of indices collected will be a bit less than 50 (40, on average).

2
  • thanks, I guess that is the answer, although for relatively small numbers, the for loop is still faster. When you got to bigger arrays the arrayfun wins...
    – user2041376
    Mar 8, 2013 at 23:44
  • You are right - but you asked for vectorized code, not for fast code... I learnt something (about arrayfun()) by answering this - so thanks to you too.
    – Floris
    Mar 8, 2013 at 23:51
2

The script below will do the same thing. The matrix newInd will contain the same values that are assigned to ind and printed out by your loop.

b=10:10:50;
a=sort(randi(50,1,10));

% create shifted version of vector b to account
% for comparison between i and i-1
newB1 = b(1:end-1);
newB2 = b(2:end);

% create tiled version of a and b
newB1 = repmat(newB1',1,numel(a));
newB2 = repmat(newB2',1,numel(a));
newA = repmat(a,numel(b)-1,1);

%find linear indices that meet required conditions
LinearInd = find(newA<newB2 & newA>newB1);
%convert linear indices to subscripts
[i,newInd] = ind2sub(size(newA),LinearInd);
% display indices that correspond to ind
newInd
1
  • I appreciate your effort but this code is an order of magnitude slower than the for loop one...
    – user2041376
    Mar 8, 2013 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.