Select subset of dataset.
Mostrar comentarios más antiguos
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)
Let's say your matrix is named 'myMat' and you want all rows where column 2 equals 3.
subSection = myMat(myMat(:,2) == 3, :);
Guillaume
el 2 de Jul. de 2018
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.
Categorías
Más información sobre Managing Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!