MATLAB: Logical Indexing into 2d matrix

logical indexing

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…)

Best Answer

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),:)