Azzera filtri
Azzera filtri

Whenever I run this function, all is good if A(i)==N. However when it is not found, the function seems to run super slowly that i never end up getting the output of 0. Do I need to preallocate somewhere? If so, what would it be?

3 visualizzazioni (ultimi 30 giorni)
"Write a function, Finder, that receives an array of numbers A and a number N and returns the position of N within the array A or the value zero if N is not present within A."
function pos=Finder(A,N)
found=false;
while ~found
for i=1:length(A);
if A(i)==N;
found=true;
break
end
end
end
if found
pos=i;
else
pos=0;
end
end

Risposte (1)

Walter Roberson
Walter Roberson il 11 Set 2017
You should use either a while loop or a for loop, but not both.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by