Select subset of dataset.
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (2)
Guillaume
le 2 Juil 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.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!