How to shorten code for determining mean/med of columns in Excel sheet?

1 view (last 30 days)
I am trying to find the mean, and median values for each of the columns from data on an excel sheet and inputting them into a matrix. I was wondering if there was any way to shorten the code into a fewer amount of lines, as continuing this process for several columns would require lots of code.
My code is shown below. I greatly appreciate any help! Thank you for your time!
For example: For an excel file named 'Popularity of TV Show Channels'
The columns to be evaluated include: THE_CW, CBS, Health&Fitness, TMC, Disney, Cartoon_Network, Nickelodeon, ABC, and ESPN.
%Form a column matrix for all means:
mean_matrix= [mean2(THE_CW); mean2(NBC);mean2(CBS);
mean2(Health&Fitness); mean2(TMC);mean2(Disney);
mean2(Catroon_Network);mean2(Nickelodeon);mean2(ABC);
mean2(ESPN)];
% Form a column matrix for all medians:
med_matrix=[median(The_CW);median(NBC);median(CBS);
median(Health&Fitness);median(TMC);median(Disney);
median(Catroon_Network); median(Nickelodeon);
median(ABC); median(EPSN)];

Answers (1)

Darius
Darius on 14 Feb 2015
Depending on how you have loaded in the matrix, you could just use the mean function on all of the data. Assuming the data for one variable is in a column and each column is another variable in the spreadsheet, then you could just load in all of the data into one big matrix (lets say A). Then you can just do:
B=mean(A)
Which will find the mean of each column to give you a row matrix. From here you can just transpose the matrix to get a column matrix of means
C=B'
You can also utilize the same method for median. Hope this helps!

Community Treasure Hunt

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

Start Hunting!