Descriptive Statistics of entire array

4 次查看(过去 30 天)
I am working on a project where I need to create a script that at one point returns the mean, median, mode, variance, standard deviation, minimum, maximum, and count of a user inputted array. The inputted array will either be a single column of an undetermined amount of numbers, or two columns of an undetermined amount of numbers.
To use the mean function as an example, if I have a matrix
x = [1,4;5,8;3,1;7,2;4,2];
then the mean function returns a mean of each column:
mean(x) = [4,3.4];
I need a mean of ALL the elements of the matrix. So a quick fix of this would be the mean2 function:
mean2(x) = 3.7;
This is great and returns exactly what I need, except I also need to find the median, mode, variance, etc. As far as I can tell, I can't just add the number 2 to the end of the functions that produce these statistical quantities to get the same result. So I need a way to either convert the user inputted matrix into a single column matrix (without losing the original matrix) and using the statistical analysis functions that way, or just a simple way to find these analyses of every single element of the array, not just the columns at single times.

回答(2 个)

John D'Errico
John D'Errico 2017-4-13
So, you can't just compute
mean(x(:))
mode(x(:))
etc?

Andrii Mishchenko
Andrii Mishchenko 2017-8-12
Hello Dear, Matthew. You can use linear indexation in order to solve your problem. Let's consider your example with a matrix
x = [1,4;5,8;3,1;7,2;4,2];
If you will create a new matrix X = x(1:end) than you will obtain a row vector X containing all elements of your original matrix x. So you can perform a basic descriptive operations on this vector and calculations will be done considering all the values of the matrix x at the same time.
Cheers!

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by