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:
The challenge is to obtain the same vector as I, following two rules:
- 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.
- 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()
N = 300;
Tm = [0 0];
bf = 0;
Z = 6;
for ii = 1:N
SA = ceil(rand(1,ceil(rand*Z))*(Z+1));
A = false(SA);
A(1) = true;
RA = ceil(rand(1,ceil(rand*Z))*(Z+1));
I = find(repmat(A,RA));
Tm(1) = Tm(1) + toc;
Imf = hdchallenger(SA,RA);
Tm(2) = Tm(2) + toc;
disp(' Unequal results, please try again.')
bf = 1;
RT = Tm/min(Tm);
if RT(2)==1 && ~bf
fprintf('You passed! You beat FIND by a factor of: %.1f!\n',RT(1))
fprintf('Your code returns good results, but is slow. Keep trying!\n')