0

I have a 3 by 3 by 2 by 3 array X containing 1s or 0s. Picture this as a row of three 3 by 3 matrices along a row and another such 'layer' behind them (from the '2').

I want to find the positions in X of the 0s in the first layer and second layer separately. I'm not really sure how to do this with find, but heuristically something like:

A = find(X == 0 & 3rd index of X is 1) 

B = find(X == 0 & 3rd index of X is 2)

EDIT

I just realised my attempt to simplify my actual question made it misleading. The array X actually has -1's, 1's and -2's and I want to find the -2's. They're not meant to be logical operators. Also I would prefer any operation proposed to be as fast as possible as this will be part of a recursive backtracking algorithm.

3
  • Do you want the returned indices to correspond to your 3x3x2x3 array, or just to the 3x3x3 subarray independently? Commented Sep 20, 2015 at 17:48
  • 1
    I want the indices to correspond to the original array please Commented Sep 20, 2015 at 17:59
  • Then Daniel's your guy. Commented Sep 20, 2015 at 18:02

2 Answers 2

2

solution using logical indexing

I recommend to use logical indexing instead of find. This gives you all indices where X is 1

value_you_want=-2
C=X==value_you_want;

Now you want only parts of these indices in A and B, first initialize A and B with false of the same size as C:

A=false(size(C));
B=A;

And finally copy the slice you want to each of these matrices:

A(:,:,1,:)=C(:,:,1,:);
B(:,:,2,:)=C(:,:,2,:);

If you really want your numeric indices, use find(A) and find(B)


Alternative solution using linear indices and find

%get all indices
C=find(X==value_you_want)
%convert linear indices to subscript indices, only use third dimension
[~,~,S,~]=ind2sub(size(X),find(X==0));
%Use S to split C
A=C(S==1);
B=C(S==2);
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, just what I was looking for:)
And of course if you don't need the actual indices, this is much faster than find.
sorry again, I realised my question is misleading, actually X contains -1, 1 and -2's. I want to find the -2's. (I changed the question to simplify things but they're not meant to be logical operators).
@dcwang: Using logical indexing is not limited to logical matrices. It is a indexing techique which applies to all matrices. I updated my answer to be more clear, insert whatever number you want for value_you_want
0

Generally use find(condition) to return linear indices in the array satisfying condition.

A = find(A(:,:,1,:)<1)

B = find(A(:,:,2,:)<1)

3 Comments

Note that OP is looking for the zeros. And that find automatically looks for nonzeros:)
Note that X(B) will not return the zeros. h=X(:,:,2,:);h(B) will return the zeros
@Daniel and the same goes for Adriaan's answer. I'm not sure, however, which one is needed by OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.