FAQ
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 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.

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
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

## Related Discussions

 view thread | post posts ‹ prev | 2 of 9 | next ›
Discussion Overview
 group r-help categories r posted Oct 27, '15 at 6:43p active Nov 4, '15 at 5:19p posts 9 users 5 website r-project.org irc #r

### 5 users in discussion

Content

People

Support

Translate

site design / logo © 2018 Grokbase