Extract a sub-matrix if the first or second column contains a specific number

조회 수: 1 (최근 30일)
I have a large matrix A, I want to extract submatrices B1, B2.. etc that contains the same number of columns as in A but only rows for which the first or second columns of A contains a specific number. For instance for this A matrix;
A=[1 4 0 0.0576 0 250 250 250;
4 5 0.017 0.092 0.158 250 250 250;
5 6 0.039 0.17 0.358 150 150 150;
3 6 0 0.0586 0 300 300 300;
6 7 0.0119 0.1008 0.209 150 150 150;
7 8 0.0085 0.072 0.149 250 250 250;
8 2 0 0.0625 0 250 250 250;
8 9 0.032 0.161 0.306 250 250 250;
9 4 0.01 0.085 0.176 250 250 250
]
Extract B such that it contains rows for which column 1 and column 2 in A contain the number '4' B4 should look like this
B4=[1 4 0 0.0576 0 250 250 250;
4 5 0.017 0.092 0.158 250 250 250;
9 4 0.01 0.085 0.176 250 250 250;
]
I have very huge matrices, I am wondering if there is a super efficient way to do this.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 7월 9일
편집: Andrei Bobrov 2017년 7월 9일
ii = 4;
Bii = A(any(A == ii,2),:);
  댓글 수: 7
puttogether
puttogether 2017년 7월 10일
Never mind I figured it out. Thanks for all the assist!!
Andrei Bobrov
Andrei Bobrov 2017년 7월 10일
[ii,jj] = find(squeeze(any(A(:,1:2) == reshape(0:9,1,1,[]),2))); % R2016b and later
B = accumarray(jj,ii,[],@(x){A(x,:)});

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by