MATLAB: Hump-day Challenger – MATLAB Indexing

challengerindexingpuzzlerepmat

This challenger is very easy to state:
Given an array of random size and dimensions whose only nonzero element is the first element, find the locations of the nonzero elements of the array returned by a random call to the REPMAT function.
Now for the details. Provided below is a test code for use in testing your solution to the challenger. In the code an array A is created, with random size and number of dimensions held in the vector SA. Another vector RA is created to be used as the second argument to REPMAT. Then a call is made to find the locations of the non-zero elements of the repmatted array using:
I = find(repmat(A,RA));
The challenge is to obtain the same vector as I, following two rules:
  1. Your function must not reproduce the repmatted array, or any array of equal or larger size, either implicitly or explicitly, in any form. What this means practically is that your function will succeed when REPMAT fails due to either an out of memory problem or a maximum variable size exceeded problem.
  2. Your function should be faster than the call to FIND and REPMAT shown above.
For those who are indexing experts this may not be much of a challenge, but for those who are still learning this could be very challenging indeed. I think one could learn a lot by completing the challenge, even if it is difficult, so keep trying! Study the test function below to understand the problem better and see how your code will be used (input args, etc). Remember, when testing your solution run the test function more than once in a row to get good timing results. Good luck. And don't forget to vote up good answers! .
.
.
function [] = test_solutions()
% Use to test an Answer for the Hump-Day Challenger.
N = 300; % The number of loop iterations.
Tm = [0 0]; % To hold the timings.
bf = 0; % Flag to check if user code passed.
Z = 6; % If you have consistent memory problems, lower this...
for ii = 1:N
% First make our array, and decide how to repmat it.
SA = ceil(rand(1,ceil(rand*Z))*(Z+1)); % The size of A.
A = false(SA);
A(1) = true;
RA = ceil(rand(1,ceil(rand*Z))*(Z+1)); % The vector for REPMAT.
% Now compare speeds.
tic
I = find(repmat(A,RA));
Tm(1) = Tm(1) + toc;
tic
Imf = hdchallenger(SA,RA); % Your func should take only SA and RA.
Tm(2) = Tm(2) + toc;
if ~isequal(I(:),Imf(:))
disp(' Unequal results, please try again.')
bf = 1;
break
end
end
RT = Tm/min(Tm); % The relative timing.
if RT(2)==1 && ~bf
fprintf('You passed! You beat FIND by a factor of: %.1f!\n',RT(1))
elseif ~bf
fprintf('Your code returns good results, but is slow. Keep trying!\n')
end

Best Answer

function idx = hdchallenger(SA,RA)
%%SCd 03/30/2011
%Updated
%Adjust for scalars, zeros, and different lengths (pad with ones)
if isscalar(RA)
RA = [RA RA];
end
if isscalar(SA)
SA = [SA SA];
end
lenRA = length(RA);
lenSA = length(SA);
if lenRA<lenSA
RA(lenSA) = 1;
RA(RA==0) = 1;
end
%Engine
n = prod(RA);
didx = SA(1)*ones(n-1,1); %preallocate
for ii = 2:lenRA;
skip = n/prod(RA(ii:end));
if ii > lenSA; %Don't exceed SA!
break
end
mpart = zeros(1,ii);
mpart(ii) = -1; %remove present 1 dim from count
didx(skip:skip:end) = prod([RA(1:ii-1),(SA(1:ii)+mpart)])+didx(skip:skip:end);
end
idx = cumsum([1; didx]); %integrate!
Related Question