Re: [R] reduce matrix

2009-11-18 Thread Karl Ove Hufthammer
On Wed, 18 Nov 2009 04:18:08 -0800 (PST) lloyd barcza 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

Re: [R] reduce matrix

2009-11-18 Thread Dimitris Rizopoulos
one way is the following: A <- rbind(c(1,0,0,3), c(0,1,2,0), c(0,0,3,5)) t(apply(A, 1, function (x) x[x != 0])) I hope it helps. Best, Dimitris lloyd barcza 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;

[R] reduce matrix

2009-11-18 Thread lloyd barcza
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