Calculate only the average value of the columns

I have a function that calculates the average of two columns of a matrix. For example, if the following matrix is ​​as follows:

inputMatrix = 1 2 5 3 9 4 6 2 3 2 4 4 3 9 1 

... And my team:

 outputVector = mean(inputArray(:,1:2)) 

... Then my conclusion:

 outputVector = 3 4 

The problem arises when my input matrix contains only one row (i.e. when it is a vector, not a matrix).

For example, input:

 inputMatrix = 4 3 7 2 1 

Gives output:

 outputVector = 3.5000 

I would like the same behavior to be supported regardless of the number of lines in the input. To clarify, the correct conclusion for the second example above should be:

 outputVector = 4 3 
+4
source share
2 answers

Use the second MEAN argument to indicate which dimension you want to average

 inputMatrix =[ 4 3 7 2 1] mean(inputMatrix(:,1:2),1) %# average along dim 1, ie average all rows ans = 4 3 
+13
source
 mean(blah, 1) 

See documentation: http://www.mathworks.co.uk/help/techdoc/ref/mean.html .

+5
source

All Articles