How can I find an element in a subset of a matrix as well as its index in the matrix?
Show older comments
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
More Answers (1)
Azzi Abdelmalek
on 19 Apr 2013
Edited: Azzi Abdelmalek
on 19 Apr 2013
Example
A=randi(2,10)-1
[ii,jj]=find(~A(1:3,1:3))
index=sub2ind(size(A),ii,jj)
3 Comments
Matt J
on 19 Apr 2013
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.
Azzi Abdelmalek
on 19 Apr 2013
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)
Igor
on 19 Apr 2013
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!