Select subset of dataset.

15 vues (au cours des 30 derniers jours)
Shahzaib Ahmed
Shahzaib Ahmed le 2 Juil 2018
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.

Réponses (2)

Adam Danz
Adam Danz le 2 Juil 2018
Modifié(e) : Adam Danz le 2 Juil 2018
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 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.

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by