Fork me on GitHub

MATLAB中求矩阵非零元的坐标

MATLAB中求矩阵非零元的坐标:

方法1:

index=find(a);
[i,j]=ind2sub(size(a),index);
disp([i,j])

 

方法2:

[i,j]=find(a>0|a<0) %列出所有非零元的坐标 
[i,j]=find(a==k) %找出等于k值的矩阵元素的坐标

 

所用函数简介:

IND2SUB Multiple subscripts from linear index.

IND2SUB is used to determine the equivalent subscript values
corresponding to a given single index into an array.

[I,J] = IND2SUB(SIZ,IND) returns the arrays I and J containing the
equivalent row and column subscripts corresponding to the index
matrix IND for a matrix of size SIZ.
For matrices, [I,J] = IND2SUB(SIZE(A),FIND(A>5)) returns the same
values as [I,J] = FIND(A>5).

[I1,I2,I3,...,In] = IND2SUB(SIZ,IND) returns N subscript arrays
I1,I2,..,In containing the equivalent N-D array subscripts
equivalent to IND for an array of size SIZ.

Class support for input IND:
float: double, single

See also sub2ind, find.

 

FIND Find indices of nonzero elements.

I = FIND(X) returns the linear indices corresponding to
the nonzero entries of the array X. X may be a logical expression.
Use IND2SUB(SIZE(X),I) to calculate multiple subscripts from
the linear indices I.

I = FIND(X,K) returns at most the first K indices corresponding to
the nonzero entries of the array X. K must be a positive integer,
but can be of any numeric type.

I = FIND(X,K,'first') is the same as I = FIND(X,K).

I = FIND(X,K,'last') returns at most the last K indices corresponding
to the nonzero entries of the array X.

[I,J] = FIND(X,...) returns the row and column indices instead of
linear indices into X. This syntax is especially useful when working
with sparse matrices. If X is an N-dimensional array where N > 2, then
J is a linear index over the N-1 trailing dimensions of X.

[I,J,V] = FIND(X,...) also returns a vector V containing the values
that correspond to the row and column indices I and J.

Example:
A = magic(3)
find(A > 5)

finds the linear indices of the 4 entries of the matrix A that are
greater than 5.

[rows,cols,vals] = find(speye(5))

finds the row and column indices and nonzero values of the 5-by-5
sparse identity matrix.

See also sparse, ind2sub, relop, nonzeros.

Overloaded methods:
codistributed/find

 

SUB2IND Linear index from multiple subscripts.

SUB2IND is used to determine the equivalent single index
corresponding to a given set of subscript values.

IND = SUB2IND(SIZ,I,J) returns the linear index equivalent to the
row and column subscripts in the arrays I and J for a matrix of
size SIZ.

IND = SUB2IND(SIZ,I1,I2,...,IN) returns the linear index
equivalent to the N subscripts in the arrays I1,I2,...,IN for an
array of size SIZ.

I1,I2,...,IN must have the same size, and IND will have the same size
as I1,I2,...,IN. For an array A, if IND = SUB2IND(SIZE(A),I1,...,IN)),
then A(IND(k))=A(I1(k),...,IN(k)) for all k.

Class support for inputs I,J:
float: double, single

See also ind2sub.



来自:http://zhidao.baidu.com/link?url=imuLeLDv16Bsg-xcu4O1M8hVzjdCiRfq6PYHu4MR-o1q9AeMWy-UM3AdZTA7av9k1WLExeyYPVDumS46TNNr1q

posted @ 2016-03-31 18:32  wangduo  阅读(863)  评论(0编辑  收藏  举报