Are there speedier alternatives to the mode function?
1 view (last 30 days)
Show older comments
I'm trying to sort through a very large matrix (12x4^12). It's actually a matrix generated by combinator of all possible permutations with repetitions of choosing 12 from 4 , though that's not critical to this question. What I need to do is identify all columns in which each number is repeated no more than x times (say x = 3 here). Here's simplified sample code:
x = 3;
poss = randi(4,12,4^12); % in my code: poss = combinator(4,12,'p','r')';
[M,F] = mode(poss,1);
evenposs = poss(:, F<=x );
My problem is that the third line above (with mode) is very slow, over 400 seconds on my computer, so it seems that doing it through the mode function is highly inefficient. Obviously I could do this with a for loop, but I've been trained to avoid that in Matlab - I thought this mode function might have been a crafty way to avoid the loop, but now I'm wondering if there are still craftier ways of achieving this.
Bonus points (i.e. my eternal gratitude) if anybody can suggest a way of using combinator or another package to generate permutations with limited repetitions, so that I can avoid this filtering step entirely.
0 Comments
Accepted Answer
Roger Stafford
on 9 Jul 2014
You could attempt to take advantage of the fact that elements in 'poss' are positive integers and can be used as indices. However, I tend to doubt that in a span of only 12 elements in each column of 'poss' the following code could outperform Mathworks' 'mode', but you can try it and see.
subs = [poss(:),reshape(repmat(1:4^12,12,1),[],1)];
F = max(accumarray(subs,1,[4,4^12]),[],1);
evenposs = poss(:,F==x); % <-- Or do you mean F<=x?
7 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!