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.

답변 (2개)

Adam Danz
Adam Danz 2018년 7월 2일
편집: Adam Danz 2018년 7월 2일

0 개 추천

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 2018년 7월 2일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2017a

태그

질문:

2018년 7월 2일

답변:

2018년 7월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by