Select subset of dataset.

I have a dataset of 176r rows and 14 columns. I want find subset of dataset on basis of value of 2nd column. Second column looks like this. 2nd column contains 16 times 1 then 16 times 3 then 16 times 2 and so on till 11. I want to extract separate data when value is 1 , 2,3 and so on.

Respuestas (2)

Adam Danz
Adam Danz el 2 de Jul. de 2018
Editada: Adam Danz el 2 de Jul. de 2018

0 votos

Let's say your matrix is named 'myMat' and you want all rows where column 2 equals 3.
subSection = myMat(myMat(:,2) == 3, :);
Guillaume
Guillaume el 2 de Jul. de 2018

0 votos

To separate the matrix in different cells of a cell array based on the value of the second column:
[~, ~, id] = unique(yourmatrix(:, 2));
result = accumarray(id, 1:size(yourmatrix, 1), [], @(rows) {yourmatrix(rows, :)});
However, whatever processing you want to do later on would most likely be much easier if you didn't split your matrix. For example, if you wanted to calculate the mean of column 1 for identical column 2 values:
meancol1bycol2 = accumarray(id, yourmatrix(:, 1), [], @mean);
Note that if values in column 2 are strictly positive integer values from 1 to n, then you don't need the unique call and can just pass yourmatrix(:, 2) instead of id.

Productos

Versión

R2017a

Etiquetas

Preguntada:

el 2 de Jul. de 2018

Respondida:

el 2 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by