find the sum by function

조회 수: 6 (최근 30일)
Eyad Ramadan
Eyad Ramadan 2020년 7월 29일
댓글: Johannes Hougaard 2020년 8월 12일
find the sum by function
  댓글 수: 1
Sriram Tadavarty
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
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:
function [total]=grade_eyad(v)
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];
total=grade_eyad(v)
  댓글 수: 1
Eyad Ramadan
Eyad Ramadan 2020년 7월 29일
편집: Eyad Ramadan 2020년 7월 29일
how i can replace the minimum grade by the average of grades , then calculate the sum of the grades ?

댓글을 달려면 로그인하십시오.


Johannes Hougaard
Johannes Hougaard 2020년 7월 29일
function total = grade_eyad(v)
[~,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)
function total = grade_eyad(v)
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
Eyad Ramadan
Eyad Ramadan 2020년 7월 30일
편집: Eyad Ramadan 2020년 7월 30일
when i use this work
function total = grade_eyad(v)
v=[12 9 6 10 13];
v(v == min(v)) = mean(v);
total = sum(v);
end
i do run , the result
>> grade_eyad
ans =
54
but i need total apper , not ans
this
>> grade_eyad
total =
54
how i can do this?
Johannes Hougaard
Johannes Hougaard 2020년 8월 12일
save the function as grade_eyad.m
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];
total = grade_eyad(v)
w = [12 8 5 8 8 6 8 10 13 10 11];
total_w = grade_eyad(v)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by