The find Function
May 9th, 2010 Posted in Functions
The find function determines the indices of array elements that meet a given logical
condition. In its simplest form, find returns a column vector of indices. Transpose that
vector to obtain a row vector of indices. For example,
1 | k = find(isprime(A))' |
picks out the locations, using one-dimensional indexing, of the primes in the magic square.
1 2 | k =
2 5 9 10 11 13 |
Display those primes, as a row vector in the order determined by k, with
1 2 3 | A(k) ans = 5 3 2 11 7 13 |
When you use k as a left-hand-side index in an assignment statement, the matrix structure
is preserved.
Leave a Reply
You must be logged in to post a comment.