Merge vector and matrix based on values in the vector?

Hello,
For a college assignment I'm trying to implement the K-Means algorithm. I know this is readily available in Matlab however I have to make a different version of it and thus need to implement it myself.
So here is the question. I'm currently busy with the step that moves the centroid to the mean distance of all the instances that get assigned to the cluster centroids.
Now I have a (collumn)vector which represents the instances that get assigned to a certain centroid. This vector contains 0's en 1's 1's represent the instances assigned to that specific centroid.
These values are aligned with a matrix which contains all the instances and their features in the sense that their indexes represent each other. So a 1 at index 12 means that row 12 of the matrix (thus instance 12) is assigned to that centroid. A 0 ofcourse means that it is assigned to a different centroid.
No what I want to do is use that vector to create a new matrix. A matrix in which the rows of original matrix that are aligned with a 0 are removed from the matrix. Those with a 1 will stay in the matrix and in a sense will be merged with the vector where the values are 1.
Say I have this vector: [0;1;1;0] and this matrix: [1,13,20,15,1,6,12,0; 5,2,3,4,5,67,1,8; 1,13,14,15,1,6,8,0; 99,2,13,4,5,67,45,8]
I would like to end up with this matrix: [5,2,3,4,5,67,1,8; 1,13,14,15,1,6,8,0]
I hope I've been clear enough.
Is there such an operation?

 Respuesta aceptada

Matt Kindig
Matt Kindig el 25 de Abr. de 2012
Use logical indexing:
a=logical([0;1;1;0]);
b=[1,13,20,15,1,6,12,0; 5,2,3,4,5,67,1,8; 1,13,14,15,1,6,8,0; 99,2,13,4,5,67,45,8];
c=b(a,:); %this is your desired output
Edit: forgot to force a to be logical.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Abr. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by