开发者

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?


Try this:

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

Source: find, ind2sub


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜