How do I find first index of a value in an array?

14 views (last 30 days)
I have the skeleton code
Function i = find_first(array, value)
% FIND_FIRST Find first index of a value in an array.
% I = FIND_FIRST(ARRAY, VALUE) returns the index I into the ARRAY where the
% first occurrence of VALUE is found. If VALUE is not in ARRAY, then an error is raised.
n = length(array);
i = 1;
while ??? && ???
i = i + 1;
end
%Check if value was not found
if ???
error('VALUE was not found.');
end
I can only use this model to answer the question. I have tried for a couple hours and I just can't crack it. Any help / tips would be appreciated :).
  5 Comments
Stephen23
Stephen23 on 20 May 2018
Edited: Stephen23 on 20 May 2018
Using length is a really bad idea. Whoever wrote that template has done a lot of beginners a disservice by teaching them to use a function that would be better replaced by numel or size. In this case numel would be much better, and would actually loop over all elements of the input array. Unlike length.
KALYAN ACHARJYA
KALYAN ACHARJYA on 20 May 2018
Exactly what is looking for, is this? value=array(1);

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!