Re: [R] extract a subset of non-contiguous elements of a matrix
Hi You can pad some NA to the result before transforming it to required matrix but as Frede pointed out, what do you want to do with such result? tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) xx-which(tm.1 0, arr.ind=TRUE) res-apply(xx, 1, paste0, collapse=,) dim(res)-c(3,2) res [,1] [,2] [1,] 1,1 1,3 [2,] 2,1 2,3 [3,] 3,1 3,3 tm.1 - matrix(c(11,22,33,-4), ncol=2) xx-which(tm.1 0, arr.ind=TRUE) res-apply(xx, 1, paste0, collapse=,) res-c(res, NA) dim(res)-c(2,2) res [,1] [,2] [1,] 1,1 1,2 [2,] 2,1 NA Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of carol white Sent: Thursday, June 19, 2014 3:46 PM To: Frede Aakmann Tøgersen; Bart Kastermans Cc: r-help@r-project.org Subject: Re: [R] extract a subset of non-contiguous elements of a matrix I realize that that the problem arises if there is a different number of negative numbers in the rows and columns of the original matrix. In this case, the resulting matrix won't have the same number of rows for all columns. The problem for ex doesn't arise for my example but for Bart's example [1,] 1 1 [2,] 2 1 [3,] 1 2 how to combine these elemnts? If the 2nd col contains the number of the resulting matrix, then, the number of rows are different and the matrix can't be completed. On Thursday, June 19, 2014 3:39 PM, Frede Aakmann Tøgersen fr...@vestas.com wrote: As Peter and Bart I really have a problem understanding you. Perhaps if you tell us what your desired result is going to used for we can be more helpful. You can do that using your latest example. In that example you want a matrix of sets of row and column indices. This will probably have to be a matrix of characters. There you go from a 3×4 matrix to a 3×2 matrix. What do you want in case of Barts 2×2 matrix? A 3×1 or 1×3 matrix? And in a more general case? Best regards Frede Sendt fra Samsung mobil Oprindelig meddelelse Fra: carol white Dato:19/06/2014 15.18 (GMT+01:00) Til: Bart Kastermans Cc: r-help@r-project.org Emne: Re: [R] extract a subset of non-contiguous elements of a matrix tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) which(tm.1 0, arr.ind=TRUE) row col [1,] 1 1 [2,] 2 1 [3,] 3 1 [4,] 1 3 [5,] 2 3 [6,] 3 3 so the answer should have the elements of tm.1 with the following indexes 1,1 1,3 2,1 2,3 3,1 3,3 On Thursday, June 19, 2014 3:08 PM, Bart Kastermans kaste...@kasterma.net wrote: 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(m0, 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 [[alternative HTML version deleted]] [[alternative HTML version deleted]] Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně
[R] extract a subset of non-contiguous elements of a matrix
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(m0, 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? thanks, Carol [[alternative HTML version deleted]] __ 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
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(m0, 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
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(m0, 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 [[alternative HTML version deleted]] __ 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
This _was_ in the answer you got, but to clarify things, perhaps try this: (M - matrix(1:9,3,3)) (ix - rbind(c(3,2),c(1,3))) M[3,2] M[1,3] M[ix] -pd 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(m0, 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 [[alternative HTML version deleted]] __ 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. -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
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(m0, 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
 tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) which(tm.1 0, arr.ind=TRUE)     row col [1,]  1  1 [2,]  2  1 [3,]  3  1 [4,]  1  3 [5,]  2  3 [6,]  3  3 so the answer should have the elements of tm.1 with the following indexes 1,1 1,3 2,1 2,3 3,1 3,3 On Thursday, June 19, 2014 3:08 PM, Bart Kastermans kaste...@kasterma.net wrote: 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(m0, 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 [[alternative HTML version deleted]] __ 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
On 19 Jun 2014, at 15:16, carol white wht_...@yahoo.com wrote: tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) which(tm.1 0, arr.ind=TRUE) row col [1,] 1 1 [2,] 2 1 [3,] 3 1 [4,] 1 3 [5,] 2 3 [6,] 3 3 so the answer should have the elements of tm.1 with the following indexes 1,1 1,3 2,1 2,3 3,1 3,3 In that example: t(apply(tm.1, 1, function(x) x[x0])) [,1] [,2] [1,]12 [2,]12 [3,]12 class(t(apply(tm.1, 1, function(x) x[x0]))) [1] matrix On Thursday, June 19, 2014 3:08 PM, Bart Kastermans kaste...@kasterma.net wrote: 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(m0, 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
As Peter and Bart I really have a problem understanding you. Perhaps if you tell us what your desired result is going to used for we can be more helpful. You can do that using your latest example. In that example you want a matrix of sets of row and column indices. This will probably have to be a matrix of characters. There you go from a 3Ã4 matrix to a 3Ã2 matrix. What do you want in case of Barts 2Ã2 matrix? A 3Ã1 or 1Ã3 matrix? And in a more general case? Best regards Frede Sendt fra Samsung mobil Oprindelig meddelelse Fra: carol white Dato:19/06/2014 15.18 (GMT+01:00) Til: Bart Kastermans Cc: r-help@r-project.org Emne: Re: [R] extract a subset of non-contiguous elements of a matrix tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) which(tm.1 0, arr.ind=TRUE) row col [1,] 1 1 [2,] 2 1 [3,] 3 1 [4,] 1 3 [5,] 2 3 [6,] 3 3 so the answer should have the elements of tm.1 with the following indexes 1,1 1,3 2,1 2,3 3,1 3,3 On Thursday, June 19, 2014 3:08 PM, Bart Kastermans kaste...@kasterma.net wrote: 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(m0, 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 [[alternative HTML version deleted]] [[alternative HTML version deleted]] __ 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
I realize that that the problem arises if there is a different number of negative numbers in the rows and columns of the original matrix. In this case, the resulting matrix won't have the same number of rows for all columns. The problem for ex doesn't arise for my example but for Bart's example [1,] 1 1 [2,] 2 1 [3,] 1 2 how to combine these elemnts? If the 2nd col contains the number of the resulting matrix, then, the number of rows are different and the matrix can't be completed. On Thursday, June 19, 2014 3:39 PM, Frede Aakmann Tøgersen fr...@vestas.com wrote: As Peter and Bart I really have a problem understanding you. Perhaps if you tell us what your desired result is going to used for we can be more helpful. You can do that using your latest example. In that example you want a matrix of sets of row and column indices. This will probably have to be a matrix of characters. There you go from a 3Ã4 matrix to a 3Ã2 matrix.  What do you want in case of Barts 2Ã2 matrix? A 3Ã1 or 1Ã3 matrix? And in a more general case? Best regards Frede Sendt fra Samsung mobil Oprindelig meddelelse Fra: carol white Dato:19/06/2014 15.18 (GMT+01:00) Til: Bart Kastermans Cc: r-help@r-project.org Emne: Re: [R] extract a subset of non-contiguous elements of a matrix  tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) which(tm.1 0, arr.ind=TRUE)     row col [1,]  1  1 [2,]  2  1 [3,]  3  1 [4,]  1  3 [5,]  2  3 [6,]  3  3 so the answer should have the elements of tm.1 with the following indexes 1,1 1,3 2,1 2,3 3,1 3,3 On Thursday, June 19, 2014 3:08 PM, Bart Kastermans kaste...@kasterma.net wrote:  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(m0, 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        [[alternative HTML version deleted]] [[alternative HTML version deleted]] __ 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.
Re: [R] extract a subset of non-contiguous elements of a matrix
You are really not helpful here. Are we talking the same scientific language? Br. Frede Sendt fra Samsung mobil Oprindelig meddelelse Fra: carol white Dato:19/06/2014 15.46 (GMT+01:00) Til: Frede Aakmann Tøgersen ,Bart Kastermans Cc: r-help@r-project.org Emne: Re: [R] extract a subset of non-contiguous elements of a matrix I realize that that the problem arises if there is a different number of negative numbers in the rows and columns of the original matrix. In this case, the resulting matrix won't have the same number of rows for all columns. The problem for ex doesn't arise for my example but for Bart's example [1,] 1 1 [2,] 2 1 [3,] 1 2 how to combine these elemnts? If the 2nd col contains the number of the resulting matrix, then, the number of rows are different and the matrix can't be completed. On Thursday, June 19, 2014 3:39 PM, Frede Aakmann Tøgersen fr...@vestas.com wrote: As Peter and Bart I really have a problem understanding you. Perhaps if you tell us what your desired result is going to used for we can be more helpful. You can do that using your latest example. In that example you want a matrix of sets of row and column indices. This will probably have to be a matrix of characters. There you go from a 3Ã4 matrix to a 3Ã2 matrix. What do you want in case of Barts 2Ã2 matrix? A 3Ã1 or 1Ã3 matrix? And in a more general case? Best regards Frede Sendt fra Samsung mobil Oprindelig meddelelse Fra: carol white Dato:19/06/2014 15.18 (GMT+01:00) Til: Bart Kastermans Cc: r-help@r-project.org Emne: Re: [R] extract a subset of non-contiguous elements of a matrix tm.1=rbind(c(1,-3,2,-4), c(1,-3,2,-4),c(1,-3,2,-4)) which(tm.1 0, arr.ind=TRUE) row col [1,] 1 1 [2,] 2 1 [3,] 3 1 [4,] 1 3 [5,] 2 3 [6,] 3 3 so the answer should have the elements of tm.1 with the following indexes 1,1 1,3 2,1 2,3 3,1 3,3 On Thursday, June 19, 2014 3:08 PM, Bart Kastermans kaste...@kasterma.net wrote: 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(m0, 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 [[alternative HTML version deleted]] [[alternative HTML version deleted]] __ 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.