Problem 105. How to find the position of an element in a vector without using the find function
Write a function posX=findPosition(x,y) where x is a vector and y is the number that you are searching for.
Examples:
findPosition([1 3 5 4 2], 2) posX is 5
findPosition([1 5 8 6 7 6 0], 8) posX is 3
Solution Stats
Problem Comments
-
8 Comments
not set up right - only one input provided
Ditto Will's comment
Please corect it or remove it.
Acting as an Admin, I tried to clean up the problem. I am rescoring, but naturally some of the "correct" answers will now fail.
Lots of "solutions" use the find function. I don't get it - what's the point of doing the problem if you ignore the key instruction?
May you please add this (or something similar) to the test suite:
% Test for find usage
fid = fopen(which('test'), 'r');
c = onCleanup(@()fclose(fid));
tline = fgetl(fid);
while ischar(tline),
if strfind(tline,'find'),
error('Don''t use find');
end
tline = fgetl(fid);
end
This should work fine after you've renamed your function.
there should be more test suite. such as x =[1 2 3;4 5 6;7 8 9];y =5;then the output is [2,2]; x = [1 2 2 3]; y =2;then the output is [2;2];
The test suite needs to be very restrictive here, the amount of trivial solutions is alarming. Yan
Solution Comments
Show commentsProblem Recent Solvers2737
Suggested Problems
-
Replace NaNs with the number that appears to its left in the row.
3008 Solvers
-
Remove the small words from a list of words.
1521 Solvers
-
Project Euler: Problem 1, Multiples of 3 and 5
3345 Solvers
-
1259 Solvers
-
Getting the indices from a matrix
651 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!