How can I find an element in a subset of a matrix as well as its index in the matrix?

Hi,
I am trying to find an element in a subset of a matrix (e.g. A) and the index of that element in matrix A.
Here is an example: let A be 6x6 matrix. I want to find a zero between rows 1 and 3, and columns 1 and 3.
I tried q = (find(A(1:3,1:3) == 0)
The answer I get is an array of 1 and 0, giving me an index of each 0 in terms of the subset of A that I defined. How can I find the index in terms of A?
thank you, i.

 Accepted Answer

subset=false(size(A));
subset(1:3,1:3)=true;
indexLogical=(A==0) & subset;
q = find(indexLogical);

More Answers (1)

Example
A=randi(2,10)-1
[ii,jj]=find(~A(1:3,1:3))
index=sub2ind(size(A),ii,jj)

3 Comments

That will work only when the subset of A is a box in the upper-left corner of A. It won't work for more general subsets.
Exact. For the general case
A=randi(2,10)-1
i0=3;i1=6;j0=2;j1=6;
[ii,jj]=find(~A(i0:i1,j0:j1))
index=sub2ind(size(A),ii+i0-1,jj+j0-1)

Sign in to comment.

Categories

Asked:

on 19 Apr 2013

Community Treasure Hunt

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

Start Hunting!