rick
rick

Reputation: 23

Matlab find does not work with high dimensional array?

say A = rand(2,2,2); [a,b,c] = find(A == A(1,2,2))

I got a=1 b=4 c=1

what?

Upvotes: 2

Views: 1882

Answers (4)

gnovice
gnovice

Reputation: 125874

The outputs from the FIND function are two sets of indices (a and b) and the values at those indices (c). For matrices greater than 2 dimensions, the second index will be a linear index.

In your example, you create a logical array when you do A == A(1,2,2). This logical array, which has a value of 1 (i.e. true) at index (1,2,2), is passed to the FIND function. The position of this non-zero value is in the first row of the matrix (output a = 1) and the fourth linear index within the remaining dimensions (output b = 4). The non-zero value of 1 is output for c.

Upvotes: 0

Steve Tjoa
Steve Tjoa

Reputation: 61134

Try this:

[a,b,c] = ind2sub(size(A), find(A==A(1,2,2)))

Source: find, ind2sub

Upvotes: 5

yuk
yuk

Reputation: 19880

Use equality == instead of assignment operator =.

A = rand(2,2,2); [a,b,c] = find(A == A(1,2,2))

See documentation for FIND. output arguments are not for all directions, only rows and columns. It seems MATLAB concatenate the 3rd direction along the 2nd and returns 4th column. The last argument equals 1 because you have only one match.

Upvotes: 0

MarkD
MarkD

Reputation: 4954

Find only works as you're trying to apply it for 2 dimensional arrays.

There are a few functions available over at Matlab Central that will do n-dimensional arrays.

Upvotes: 0

Related Questions