Need help writing a matlab function.
27 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Image Analyst
2024-11-17,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 个评论
Walter Roberson
2024-11-17,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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!