Reputation: 891
So I have a = rand(3,3,2).
Then I have a logical index that's something like
b = [1 0 0; 0 0 1; 1 1 1]
b =
1 0 0
0 0 1
1 1 1
But I want to be able to call a(b) for both a(:,:,1) and a(:,:,2). Both a(:,:,1) and a(:,:,2) have shared logical indices. How would I be able to do this?
Upvotes: 0
Views: 61
Reputation: 54
a1 = a(:,:,1);
a2 = a(:,:,2);
selected_a1 = a1(b==1);
selected_a2 = a2(b==1);
Here, we select all values of matrix a where b is 1 and then store them into selected_a1, and selected_a2.
Upvotes: 2
Reputation: 47392
If b
is a logical array then you can do
n = size(a, 3);
a(repmat(b, [1,1,n]))
Upvotes: 1
Reputation: 272557
Assuming b
is a logical array (if it's not, then convert by doing b = logical(b);
), then try the following:
a([b(:); b(:)])
Upvotes: 1