# Octave code example

This is one of the simpler code tasks I worked on. I use ‘R’ but since this is Octave my first attempt wasn’t generic. The for loop assumes that there are 2 columns in the matrix shown below. There is a way to modify it to be more generic but this an example to show my Octave skills !!

I have a matrix with 2 columns and a vector which has repeating sets of the numbers 3,2 and 1. The task is to calculate the mean of all values in each column of the matrix rows that are grouped by the numbers 3, 2 and 1. So I decided to add the vector as the 3rd column of the matrix.

### Matrix $\begin{pmatrix} 0.5403023 & -0.9576595 \\ -0.4161468 & -0.2751633 \\ -0.9899925 & 0.6603167 \\ -0.6536436 & 0.9887046 \\ 0.2836622 & 0.4080821 \\ 0.9601703 & -0.5477293 \\ 0.7539023 & -0.9999608 \\ -0.1455000 & -0.5328330 \\ -0.9111303 & 0.4241790 \\ -0.8390715 & 0.9912028 \\ 0.0044257 & 0.6469193 \\ 0.8438540 & -0.2921388 \\ 0.9074468 & -0.9626059 \\ 0.1367372 & -0.7480575 \\ -0.7596879 & 0.1542514 \end{pmatrix}$

### Vector $\begin{pmatrix} 2 \\ 3 \\ 1 \\ 2 \\ 3 \\ 1 \\ 2 \\ 3 \\ 1 \\ 2 \\ 3 \\ 1 \\ 2 \\ 3 \\ 1 \end{pmatrix}$

Add the vector as the 3rd column.

centroids = zeros(K, n);%Declare matrix to store the result
X(:,end + 1 ) = idx; $\begin{pmatrix} 0.5403023 & -0.9576595 & 2.0000000 \\ -0.4161468 & -0.2751633 & 3.0000000 \\ -0.9899925 & 0.6603167 & 1.0000000 \\ -0.6536436 & 0.9887046 & 2.0000000 \\ 0.2836622 & 0.4080821 & 3.0000000 \\ 0.9601703 & -0.5477293 & 1.0000000 \\ 0.7539023 & -0.9999608 & 2.0000000 \\ -0.1455000 & -0.5328330 & 3.0000000 \\ -0.9111303 & 0.4241790 & 1.0000000 \\ -0.8390715 & 0.9912028 & 2.0000000 \\ 0.0044257 & 0.6469193 & 3.0000000 \\ 0.8438540 & -0.2921388 & 1.0000000 \\ 0.9074468 & -0.9626059 & 2.0000000 \\ 0.1367372 & -0.7480575 & 3.0000000 \\ -0.7596879 & 0.1542514 & 1.0000000 \\ \end{pmatrix}$

Find the unique values of the 3rd column. The decimals do not matter here.

uidx = unique(X(:,3),"columns");


### Vector with unique elements $\begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}$

I use find to get all the row numbers(j) of the values in the matrix where the 3rd column matches each of the unique values(uidx)

for i = 1:size(uidx,1)
[j,k]=find(X(:,3) == uidx(i) );
s = X(j,1);
t = X(j,2);
centroids(i,:) = [mean(s);mean(t)]
end


### Mean of each column for all the grouped rows $\begin{pmatrix} -0.171357 & 0.079776 \\ 0.141787 & -0.188064 \\ -0.027364 & -0.100211 \end{pmatrix}$