从矩阵matlab中删除零列和行
问题描述:
我想从一个大矩阵中删除一些列和行.这些是具有全零值的列和行. MATLAB中是否有任何功能可以为您快速完成?我的矩阵稀疏.我正在这样做:
I would like to remove some columns and rows from a big matrix. Those are the columns and the rows which have all zeros values. Is there any function in MATLAB that can do it for you quite fast? My matrices are sparse. I am doing this way:
% To remove all zero columns from A
ind = find(sum(A,1)==0) ;
A(:,ind) = [] ;
% To remove all zeros rows from A
ind = find(sum(A,2)==0) ;
A(ind,:) = [] ;
为此我准备一行代码会很好,因为我可能会重复执行此类任务.谢谢
It would be nice to have a line of code for this as I may do this kind of task repeatedly. Thanks
答
单行代码为:
A=A(any(X,2),any(X,1))
不需要像以前那样使用find,您可以直接使用逻辑向量进行索引.
There is no need to use find like you did, you can directly index using logical vectors.