find the sum by function

1 ビュー (過去 30 日間)
コメント済み: Johannes Hougaard 2020 年 8 月 12 日
find the sum by function
1 件のコメント表示非表示 なし
Sriram Tadavarty 2020 年 7 月 29 日
What is looked for here? Whats the output that is expected from the sample vector? Can you show the code that you tried and where you struck?

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

回答 (2 件)

Cristian Garcia Milan 2020 年 7 月 29 日
I think that your problem is the function definition. Actually you are using total as input and v as output and I think that your intention is the opposite. The function should be:
min(v);
mean(v);
v(min(v))=mean(v);
total=sum(v)
end
When you have these code saved, you can use:
v=[12 9 6 10 13];
1 件のコメント表示非表示 なし
how i can replace the minimum grade by the average of grades , then calculate the sum of the grades ?

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

Johannes Hougaard 2020 年 7 月 29 日
[~,idx] = min(v); %finding the position of the minimum value (3 in the given example)
v(idx) = mean(v); %replacing the value (Christians code will replace the 6th element (13)
total = sum(v);
end
or you could index in another way (will be better if there are more 6'es to be replaced)
v(v == min(v)) = mean(v); % find all instances where v equals min(v) by the == operator and replace by mean(v)
total = sum(v);
end
2 件のコメント表示非表示 1 件の古いコメント
Johannes Hougaard 2020 年 8 月 12 日
Then you'll be able to call it from your workspace with an output with any name you like and with alterations to v (or use another variable name if desired).
v = [12 9 6 10 13];