Logical Indexing into 2d matrix
조회 수: 14 (최근 30일)
이전 댓글 표시
I am trying to use a logical vector to pull out all data from a 2d matrix. The data is below
67 75 52
44 23 34
59 21 30
47 22 22
52 21 46
28 53 43
22 51 11
35 58 31
24 51 20
56 66 22
30 51 30
42 23 24
55 29 68
65 27 74
65 25 52
69 42 88
58 52 31
33 52 24
38 53 39
47 30 40
My logical vector is below
1
0
0
0
0
0
0
0
0
1
0
0
1
1
0
1
1
0
1
1
So what I would like to do is pull all 3 columns of my data and create a new matrix that only has the data items where the logical vector is true (Row 1, Row 10, Row 13...)
댓글 수: 0
채택된 답변
Andrei Bobrov
2012년 1월 2일
a = [ 67 75 52
44 23 34
59 21 30
47 22 22
52 21 46
28 53 43
22 51 11
35 58 31
24 51 20
56 66 22
30 51 30
42 23 24
55 29 68
65 27 74
65 25 52
69 42 88
58 52 31
33 52 24
38 53 39
47 30 40]
v = logical([ 1
0
0
0
0
0
0
0
0
1
0
0
1
1
0
1
1
0
1
1]);
out = a(v,:)
OR
v = [ 1
0
0
0
0
0
0
0
0
1
0
0
1
1
0
1
1
0
1
1];
out = a(find(v),:)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!