Need help writing a matlab function.
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Peyton
el 17 de Nov. de 2024 a las 2:47
Respondida: Walter Roberson
el 17 de Nov. de 2024 a las 3:27
Write a function called hw4_problem3 that takes a vector containing integers called w
and a scalar, n, as its input arguments. The function returns the first element found in w that are
larger than or equal to n. If there is no element in w that is larger than or equal to n, return -1.
3 comentarios
Respuestas (2)
Image Analyst
el 17 de Nov. de 2024 a las 3:17
Replace all your [1, 2, 3; 4, 5, 6] by A.
Replace
output = hw4_problem1(A)
by
output = hw4_problem1(A, n)
A is a vector, not a matrix so don't overcomplicate it by worrying about rows and columns.
A = [1,4,9,2,4,0,8,-20]
firstIndex = find(A >= 3, 1, 'first')
value = A(firstIndex) % Get first value more than n
Please adapt this to your homework problem.
0 comentarios
Walter Roberson
el 17 de Nov. de 2024 a las 3:27
A deliberately clumsy implementation:
function appropriate_element_to_return = hw4_problem3(w, n)
array_being_indexed = w;
value_to_compare_to = n;
found_it_at_location = nan;
for index_of_array = numel(w):-1:1
if array_being_indexed(index_of_array) >= value_to_compare_to
found_it_at_location = index_of_array;
end
end
if ~isfinite(found_it_at_location)
appropriate_element_to_return = -1;
else
appropriate_element_to_return = array_being_indexed(found_it_at_location);
end
end
0 comentarios
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!