[R] Extract entries from matrix

Boris Steipe boris.steipe at utoronto.ca
Wed Oct 28 19:18:55 CET 2015


Your code does not produce the matrix in your image.
The first three rows contain all-zeros and the last row is missing.
The following line fixes that:

m <- rbind(m[-(1:3), ], 1:5)

Given that matrix, the following code produces the output
you have illustrated. It's so trivial however that I suspect
something must be missing in your problem description.



v <- numeric(nrow(m))
j <- 1
for (i in 1:nrow(m)) {
    if (j > ncol(m) || m[i,j] == 0) {
        j <- 1
    }
    v[i] <- m[i,j]
    j <- j+1
}

v

Note that this puts 0 in the output if
there is a zero in your first column and
the "diagonal". Your example didn't have that.



B.


On Oct 28, 2015, at 1:17 PM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:

> Dear all,
> 
> I thought I would better send an image illustrating that the problem is
> (hope the file gets through).  In the picture, the matrix "m" is given by
> 
> ## input
> m <- structure(c(0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2,
> 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0,
> 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
> 5), .Dim = c(12L, 5L))
> 
> We start from the entry [1,1] and select all values in the diagonal until
> we find the first zero. That is, we select the values in purple.  Because
> the first zero is found in [4,4], the next matrix begins in the 4th row.
> The values of interest would be 1, 2, 3, 4, 5 (light blue).  The last value
> in this resulting vector is entry m[8,5], which is located at the maximum
> number of columns of m. Hence, the next matrix to work with starts at row 9
> and the values of interest are 1, 2 (in orange).
> 
> The output vector would then be of the same length as the number of rows in
> "m", and would contain the elements previously selected.
> 
> Any ideas on how to proceed?
> 
> Thank you very much in advance.
> 
> Best regards,
> Jorge Velez.-
> 
> 
> 
> On Tue, Oct 27, 2015 at 4:38 PM, Jorge I Velez <jorgeivanvelez at gmail.com>
> wrote:
> 
>> Thank you all for your solutions and comments.
>> 
>> As Dr. Carlson mentioned, we leave rows 1 to 3 out as they are all zeroes.
>> Then, the entries I need to select from m are
>> 
>> ----------------
>> entry     value
>> ----------------
>> 4,1  ---> 1
>> 5,2  ---> 2
>> 6,3  ---> 3
>> 7,1  ---> 1
>> 8,2  ---> 2
>> 9,3  ---> 3
>> 10,4  ---> 4
>> 11,5  ---> 5
>> 12,1  ---> 1
>> 
>> Note that the entry [7,4] is zero, so we start from the first column in
>> the 7th row and then select entry [7,1] instead.  That's what I meant by  "...
>> the idea is to extract the diagonal elements until a zero is found."  I
>> should have said *entries* instead of  _diagonal elements_. I am sorry Dr.
>> Turner for the confusion.
>> 
>> Starting with m
>> 
>> R> m
>> #       [,1] [,2] [,3] [,4] [,5]
>> # [1,]    0    0    0    0    0
>> # [2,]    0    0    0    0    0
>> # [3,]    0    0    0    0    0
>> # [4,]    1    2    3    0    0
>> # [5,]    1    2    3    0    0
>> # [6,]    1    2    3    0    0
>> # [7,]    1    2    3    0    0
>> # [8,]    1    2    3    0    0
>> # [9,]    1    2    3    4    0
>> #[10,]    1    2    3    4    0
>> #[11,]    1    2    3    4    5
>> #[12,]    1    2    3    4    5
>> 
>> the first submatrix to work with is
>> 
>> # [4,]    1    2    3    0    0
>> # [5,]    1    2    3    0    0
>> # [6,]    1    2    3    0    0
>> 
>> from which the elements of interest are 1, 2, 3.  Note that the 7th row of
>> m is not included here because m[7, 5] = 0.
>> 
>> Further, the second submatrix is
>> 
>> # [7,]    1    2    3    0    0
>> # [8,]    1    2    3    0    0
>> # [9,]    1    2    3    4    0
>> #[10,]    1    2    3    4    0
>> #[11,]    1    2    3    4    5
>> 
>> and the corresponding elements are 1, 2, 3, 4, 5.
>> 
>> And the last matrix is
>> 
>> #[12,]    1    2    3    4    5
>> 
>> from which the position [12,1] is selected.
>> 
>> So, the resulting entries from this process are 1, 2, 3, 1, 2, 3, 4, 5, 1.
>> 
>> Thank you in advance for any additional insight you may provide.
>> 
>> Regards,
>> Jorge Velez.-
>> 
>> 
>> 
>> On Tue, Oct 27, 2015 at 4:06 PM, David L Carlson <dcarlson at tamu.edu>
>> wrote:
>> 
>>> I don't see how you are getting the result you provide.
>>> 
>>>> m
>>>      [,1] [,2] [,3] [,4] [,5]
>>> [1,]    0    0    0    0    0
>>> [2,]    0    0    0    0    0
>>> [3,]    0    0    0    0    0
>>> [4,]    1    2    3    0    0
>>> [5,]    1    2    3    0    0
>>> [6,]    1    2    3    0    0
>>> [7,]    1    2    3    0    0
>>> [8,]    1    2    3    0    0
>>> [9,]    1    2    3    4    0
>>> [10,]    1    2    3    4    0
>>> [11,]    1    2    3    4    5
>>> [12,]    1    2    3    4    5
>>>> t(sapply(1:8, function(x) diag(m[x:12, ])))
>>>     [,1] [,2] [,3] [,4] [,5]
>>> [1,]    0    0    0    0    0
>>> [2,]    0    0    3    0    0
>>> [3,]    0    2    3    0    0
>>> [4,]    1    2    3    0    0
>>> [5,]    1    2    3    0    0
>>> [6,]    1    2    3    4    0
>>> [7,]    1    2    3    4    5
>>> [8,]    1    2    3    4    5
>>> 
>>> These are all of the diagonals from the 1st through 8th rows. The first 3
>>> begin with 0 so we leave them out, but then we have 4th: 1, 2, 3; 5th: 1,
>>> 2, 3; 6th: 1, 2, 3, 4, etc so you must have some additional rule in mind to
>>> get your answer.
>>> 
>>> -------------------------------------
>>> David L Carlson
>>> Department of Anthropology
>>> Texas A&M University
>>> College Station, TX 77840-4352
>>> 
>>> 
>>> 
>>> -----Original Message-----
>>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jorge I
>>> Velez
>>> Sent: Tuesday, October 27, 2015 2:44 PM
>>> To: jim holtman
>>> Cc: R-help
>>> Subject: Re: [R] Extract entries from matrix
>>> 
>>> Dear Jim,
>>> 
>>> Thank you very much for your quick reply.
>>> 
>>> I am sorry for the confusion it may have caused, but I messed up the
>>> indexes in my example.  I would like, from the following matrix "m"
>>> 
>>> ## input
>>> m <- structure(c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L,
>>> 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 3L, 3L,
>>> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L,
>>> 4L, 4L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 5L, 5L), .Dim = c(12L,
>>> 5L))
>>> 
>>> to obtain
>>> 
>>> 1 2 3 1 2 3 4 5 1
>>> 
>>> Sure using m[idx] will give the desired result.  The problem is that idx
>>> is
>>> not known and needs to be determined from "m".  I would like to use
>>> something like
>>> 
>>> extractDiagonals(m)
>>> ## [1]  1 2 3 1 2 3 4 5 1
>>> 
>>> I look forward to your reply.  Thanks in advance.
>>> 
>>> Best regards,
>>> Jorge Velez.-
>>> 
>>> 
>>> 
>>> On Tue, Oct 27, 2015 at 2:31 PM, jim holtman <jholtman at gmail.com> wrote:
>>> 
>>>> If you want to use the numbers you gave a the index into the matrix,
>>> then
>>>> you can create a matrix with the values and then index into 'm'.  I
>>> don't
>>>> see a '4' in the output example you gave using your index values:
>>>> 
>>>>> m <- structure(c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>>> +  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 2L, 2L, 2L,
>>>> +  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L,
>>>> +  0L, 0L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
>>>> +  3L, 3L, 3L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 4L, 4L,
>>>> +  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 0L, 0L, 0L, 0L, 0L,
>>>> +  0L, 0L, 0L, 0L, 0L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
>>>> +  5L), .Dim = c(22L, 5L))
>>>>> # create index matrix
>>>>> indx <- matrix(c(4, 1,
>>>> +  5, 2,
>>>> +  6, 3,
>>>> +  7, 1,
>>>> +  8, 2,
>>>> +  9, 3,
>>>> +  10, 1,
>>>> +  11, 2,
>>>> +  12, 3), ncol = 2, byrow = TRUE)
>>>>> 
>>>>> 
>>>>> m
>>>>      [,1] [,2] [,3] [,4] [,5]
>>>> [1,]    0    0    0    0    0
>>>> [2,]    0    0    0    0    0
>>>> [3,]    0    0    0    0    0
>>>> [4,]    1    2    3    0    0
>>>> [5,]    1    2    3    0    0
>>>> [6,]    1    2    3    0    0
>>>> [7,]    1    2    3    0    0
>>>> [8,]    1    2    3    0    0
>>>> [9,]    1    2    3    4    0
>>>> [10,]    1    2    3    4    0
>>>> [11,]    1    2    3    4    5
>>>> [12,]    1    2    3    4    5
>>>> [13,]    1    2    3    4    5
>>>> [14,]    1    2    3    4    5
>>>> [15,]    1    2    3    4    5
>>>> [16,]    1    2    3    4    5
>>>> [17,]    1    2    3    4    5
>>>> [18,]    1    2    3    4    5
>>>> [19,]    1    2    3    4    5
>>>> [20,]    1    2    3    4    5
>>>> [21,]    1    2    3    4    5
>>>> [22,]    1    2    3    4    5
>>>>> indx
>>>>      [,1] [,2]
>>>> [1,]    4    1
>>>> [2,]    5    2
>>>> [3,]    6    3
>>>> [4,]    7    1
>>>> [5,]    8    2
>>>> [6,]    9    3
>>>> [7,]   10    1
>>>> [8,]   11    2
>>>> [9,]   12    3
>>>>> m[indx]
>>>> [1] 1 2 3 1 2 3 1 2 3
>>>> 
>>>> 
>>>> Jim Holtman
>>>> Data Munger Guru
>>>> 
>>>> What is the problem that you are trying to solve?
>>>> Tell me what you want to do, not how you want to do it.
>>>> 
>>>> On Tue, Oct 27, 2015 at 2:43 PM, Jorge I Velez <
>>> jorgeivanvelez at gmail.com>
>>>> wrote:
>>>> 
>>>>> Dear R-help,
>>>>> 
>>>>> I am working with a matrix "m" from which I would like to extract some
>>>>> elements.  An toy example is as follows:
>>>>> 
>>>>> ## input matrix
>>>>> m <- structure(c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>>>> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 2L, 2L, 2L,
>>>>> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L,
>>>>> 0L, 0L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
>>>>> 3L, 3L, 3L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 4L, 4L,
>>>>> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 0L, 0L, 0L, 0L, 0L,
>>>>> 0L, 0L, 0L, 0L, 0L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
>>>>> 5L), .Dim = c(22L, 5L))
>>>>> 
>>>>> R> m
>>>>> #         [,1]  [,2] [,3] [,4] [,5]
>>>>> #  [1,]    0    0    0    0    0
>>>>> #  [2,]    0    0    0    0    0
>>>>> #  [3,]    0    0    0    0    0
>>>>> #  [4,]    1    2    3    0    0
>>>>> #  [5,]    1    2    3    0    0
>>>>> #  [6,]    1    2    3    0    0
>>>>> #  [7,]    1    2    3    0    0
>>>>> #  [8,]    1    2    3    0    0
>>>>> #  [9,]    1    2    3    4    0
>>>>> # [10,]   1    2    3    4    0
>>>>> # [11,]   1    2    3    4    5
>>>>> # [12,]   1    2    3    4    5
>>>>> 
>>>>>> From "m", I would like to extract the entries
>>>>> 
>>>>> 4, 1
>>>>> 5, 2
>>>>> 6, 3
>>>>> 7, 1
>>>>> 8, 2
>>>>> 9, 3
>>>>> 10, 1
>>>>> 11, 2
>>>>> 12, 3
>>>>> 
>>>>> so at the end of applying a function "f" to "m" I get
>>>>> 
>>>>> 1, 2, 3, 1, 2, 3, 4, 1, 2, 3
>>>>> 
>>>>> 
>>>>> Basically the idea is to extract the diagonal elements until a zero is
>>>>> found.
>>>>> 
>>>>> In the real problem the dimensions of "m" are much bigger, but this
>>>>> smaller
>>>>> version of "m" illustrate what needs to be done.
>>>>> 
>>>>> I would greatly appreciate any ideas on how to do this.
>>>>> 
>>>>> Thanks in advance,
>>>>> Jorge Velez.-
>>>>> 
>>>>>        [[alternative HTML version deleted]]
>>>>> 
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> 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.
>>>>> 
>>>> 
>>>> 
>>> 
>>>        [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>> 
>> 
>> 
> <example.png>______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list