Clear Filters
Clear Filters

How do I write a function that finds the max, min, mean, median, mode, std, var, and count of an array? call it "mystat"

39 views (last 30 days)
function [min,max,mean,median,mode,var,std] = mystat(x) % Enter in an array and mystat will give the min, max, mean, median, mode, % var, and standard deviation of the array.
mean = mean(x) median = median(x) mode = mode(x) var = var(x) std = std(x) count = length(x) end
For some reason this code doesn't work when I try to call it.
  3 Comments

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 8 Sep 2016
By specifying your output arguments using the same names as the functions that you need to call to compute those outputs, you make it so MATLAB cannot call those functions. [You can't use the same name as a function and as a variable inside a function.] Change the name of the output arguments, or pack all those outputs into a struct array so that the user of your function doesn't need to call it with seven separate outputs (and doesn't need to remember the order in which you defined your function to return those outputs.)
myout = struct;
myout.mean = mean(x); % etc
  6 Comments
Steven Lord
Steven Lord on 9 Sep 2016
Nowhere in your code do you assign to a variable named k. Either change the name of the struct in which you're storing your outputs from myout to k or change the name of your output argument from k to myout. [Don't do both.]
Kyle Reagan
Kyle Reagan on 9 Sep 2016
I changed it from "function [k]" to "function [myout]" and it worked. Thanks for the help, it's been a year since I've written any functions.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!