On Wed, 18 Nov 2009 04:18:08 -0800 (PST) lloyd barcza 
<floyd...@yahoo.com> wrote:
> I am trying to reduce the dimension of matrix by removing 
> zero elements and creating a sub-matrix.
> 
> For example:
> 
> A= [1,0,0,3;  0,1,2,0; 0,0,3,5]
> 
> then the new matrix B would be:
> 
> B= [1,3;1,2;3,5]
> 
> There are the same number of zero elements in each row of A 
> so dimension of B will not be a problem.

This should work:

B=matrix(t(A)[t(A)!=0], nrow=nrow(A), byrow=TRUE)

-- 
Karl Ove Hufthammer

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to