Need help writing a matlab function.
28 ビュー (過去 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!