How to: simplify statistical commands into fewer lines of code
Ältere Kommentare anzeigen
I have an excel spreadsheet with data of different categories and am trying to find the mean, median, maximum, minimum, and standard deviation values for each category by creating a matrix for each one. (each category corresponds to its own column of data in the spreadsheet). Then I will put the data into one large matrix. So far I have coded below for the mean:
mean_matrix = [ mean2(population)
mean2(aveincome)
mean2(birthrate)
mean2(deathrate)
mean2(emprate)
mean2(familysize) ];
I was going to do the same to create the matrices for the median, min, max, and std, and eventually create the summary matrix by coding:
summary = [mean_matrix, median_matrix, min_matrix, max_matrix, std_matrix];
However I realized that the above would span over fifteen lines of code. Is there a way in which I can simplify my code in order to take the mean, etc. more effectively? Should I use cell numbers in the excel spreadsheet somehow?
Antworten (2)
Sean de Wolski
am 9 Feb. 2015
Instead of creating a matrix for each one, create a table ( readtable is the function to bring it in from Excel). Now you can use summary, grpstats or varfun to get the statistics all in one line:
T = table(rand(10,1),rand(10,2),'VariableNames',{'test1','test2'})
varfun(@mean2,T)
dpb
am 10 Feb. 2015
... [compute] mean, median, maximum, minimum, and standard deviation values for each category [in which each[ corresponds to its own column of data in the spreadsheet).
Then just read the array and operate on it...
data=xlsread('yourspreadsheet'); % read/return the numeric data
summary=[mean(x); median(x); min(x); max(x); std(x)];
That's the power of Matlab; it operates on arrays automagically.
Kategorien
Mehr zu Spreadsheets finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!