If you give an example of input and desired output I can think about this. But at this point I do not understand what you want. In the example I gave the positive elements do not form a submatrix in any way I can think of.
On 19 Jun 2014, at 15:04, carol white <wht_...@yahoo.com> wrote: > well it gives a vector which is useless as I want a matrix. > > > On Thursday, June 19, 2014 2:40 PM, Bart Kastermans <kaste...@kasterma.net> > wrote: > > > > tm.1 <- matrix(c(11,22,33,-4), ncol=2) > > which(tm.1 > 0, arr.ind=TRUE) > row col > [1,] 1 1 > [2,] 2 1 > [3,] 1 2 > > tm.1[which(tm.1 > 0, arr.ind=TRUE)] > [1] 11 22 33 > > This last command does what you ask I think. > > On 19 Jun 2014, at 14:12, carol white <wht_...@yahoo.com> wrote: > > > The extracted values don't form a matrix and that's the question how to > > extract because which returns the indexes? that is, from > > 1,1 > > 2,1 > > 1,2 > > > > how to retrieve values? > > > > Or if at the position 2,1, there is a negative value, how to retrieve > > 1,1 > > 1,2 > > > > > > Carol > > > > > > On Thursday, June 19, 2014 1:29 PM, Bart Kastermans <kaste...@kasterma.net> > > wrote: > > > > > > On 19 Jun 2014, at 13:19, carol white <wht_...@yahoo.com> wrote: > > > > > Hi, > > > Is there a way to extract a subset of non-contiguous elements of a matrix > > > elegantly and with 1 or very few scripts? > > > > > > Suppose I have a matrix of positive and negative numbers (m) and I want > > > to retrieve only the positive number. This I can do > > > > > > which(m>0, arr.ind=T) which gives the indices of positive elements like > > > (37,1), (80,1), ..., (54,2) etc. How can I extract positive numbers > > > without looping on the indexes provided by which to make a new matrix? > > > > What matrix do you want? For e.g. > > > > > tm.1 <- matrix(c(11,22,33,-4), ncol=2) > > > which(tm.1 > 0, arr.ind=TRUE) > > row col > > [1,] 1 1 > > [2,] 2 1 > > [3,] 1 2 > > > tm.1[which(tm.1 > 0, arr.ind=TRUE)] > > [1] 11 22 33 > > > > The extracted values do not form a matrix. > > > > Either the above contains the answer, or I don’t understand the question. > > > > Best, > > Bart > > > > > > > > > > thanks, > > > > > > Carol > > > > > > ______________________________________________ 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.