How to calculate mean of matrix based on column va

2019-07-27 22:15发布

问题:

I have B matrix of N*4 dim. I want calculate the mean of the matrix based on last column values. Last column has repeated values in the range of 1 to 3. I want to calculate the mean of all rows whose last column have same value. I am using this command:

l(it:,)=mean(B(i,:)) 

where it ranges from 1 to 3 in the loop and i has all the indices of rows whose last column=1.When I run this code I get Sub scripted assignment dimension mismatch error. Can anyone point out what is wrong in the command?

回答1:

Consider the following example data:

B = [ 0.4000    0.3000    0.2000    1.0000
      0.3000    0.2000    0.1000    2.0000
      0.7000    0.8000    0.6000    1.0000
      0.3000    0.4000    0.8000    2.0000
      0.7000    0.5000    0.5000    3.0000
      0.1000    0.3000    0.9000    3.0000
      0.6000    0.4000    0.5000    1.0000 ];

Two possible approaches:

  1. Using logical indexing:

    result = NaN(3,3);
    for k = 1:3
        result(k,:) = mean(B(B(:,4)==k,1:3));
    end
    
  2. Using accumarray:

    result = NaN(3,3);
    for k = 1:3
        result(:,k) = accumarray(B(:,4), B(:,k), [], @mean, NaN);
    end
    

With the example data, either of the above gives

result =
    0.5667    0.5000    0.4333
    0.3000    0.3000    0.4500
    0.4000    0.4000    0.7000


回答2:

Your question is not the clearest, but I think I know what you are trying to do.

You say that i contains the indices of the rows of interest for each value 1,2,3 so I am assuming that you require

 mean_k = mean(mean((B(i,:)))

for each k = 1,2,3. Obviously you must be recalculating i each time you iterate over 1,2,3 when identifying the relevant rows of interest.

The mean function when called on a matrix does not return a scalar, it returns a row vector whose elements are the mean of each column of the matrix. Therefore to get the mean, you need to call mean again on the result.

Bear in mind that this also includes the final column value (that you are using for classification) in your overall mean calculations