フィルターのクリア

custom function using loops and if statements help

1 回表示 (過去 30 日間)
Luke Radcliff
Luke Radcliff 2016 年 6 月 8 日
コメント済み: Rik 2020 年 6 月 4 日
I have to make a function that takes a input vector of integers(each between 1 and 100) and will output the number of elements strictly greater than 30 and calculate the sum of all the elements in the vector. It has to be made using only loops and if statements within the function, also cannot use the sum function.
function [c,s] = y(x)
?
end
x = [19 79 20 100 81 43] -> c = 4, s = 342, here is a test case and the outputs you are supposed to get. I have been trying this and I am not quite sure how to go with it.
  1 件のコメント
Rik
Rik 2020 年 6 月 4 日
In response to the flag by Joseph M Mahoney ("This is a homework question that I created and it violates my IP"):
Personally I don't see how any of this content could be classified as your intellectual property (not a lawyer, nor a Mathworks employee). Which elements should be removed to remove your IP? The choice of 1, 100, and 30? The test case? I can understand you aren't happy to find a complete solution to your homework question, but that doesn't make it an IP violation.
The other question you flagged is less clear to me, as I don't know enough of the physics involved to see if that formula could be IP.

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2016 年 6 月 8 日
function [c,s] = y(x)
n = length(x) ; % Number of elements in set x
c = 0 ; % initiliaze count for number > 30
s = 0 ; %initilaze sum
for i = 1:n % loop for each numbee
s = s+x(i) ;
if x(i)>30
c = c+1 ;
end
end
  3 件のコメント
KSSV
KSSV 2016 年 6 月 8 日
direct function to find sum is sum(x)
Luke Radcliff
Luke Radcliff 2016 年 6 月 8 日
yea, not allowed to used the sum function for this problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by