find the sum by function
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
find the sum by function
1 commentaire
Sriram Tadavarty
le 29 Juil 2020
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?
Réponses (2)
Cristian Garcia Milan
le 29 Juil 2020
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 commentaire
Johannes Hougaard
le 29 Juil 2020
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 commentaires
Johannes Hougaard
le 12 Août 2020
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)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!