Search code examples
matrixencodingoctave

Octave: Find mean in a column but only for matching values


I'm working on an extended matrix in octave where the final column is the label that states which centroid that data point is closest to. I know I need to use the mean function but am unsure how to select only specific values within the column that I need.

For example:

column 1 column 2 ... ... ... etc... label column -> (is the last column) info ..... ... ... ... ... 1 ... .... ... ... ... ... 2 ... ... ... ... ... ... 1 ... ... ... ... ... ... 3 .... .... ... ... ... ... 4

I would only want to pick the rows that are labeled 1 so that I could update their centroid, so when it is graphed they would all be clustered together in a group where 2, 3, and 4 would have each grouping of their own.


Solution

  • Here is an example:

    >> m = randi(9,10,4)
    m =
       3   8   5   1
       6   6   7   9
       5   4   9   8
       5   7   1   1
       3   8   8   4
       6   5   1   6
       2   9   9   2
       4   6   4   7
       4   4   1   1
       8   1   1   9
    
    >> m(m(:,end)==1,:)
    ans =
       3   8   5   1
       5   7   1   1
       4   4   1   1