Maybe it's just me --- I'm notoriously slow --- but I haven't a clue what you are trying to do. You said:

  "... the idea is to extract the diagonal elements until a
   zero is found."

I don't see anything that resembles a "diagonal element" (*entry*, actually; sets have elements, arrays have entries) in your desired output. What do you mean by the diagonal of a non-square matrix anyway?

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

8/10/15 08:43, Jorge I Velez wrote:
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 <jholt...@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 <jorgeivanve...@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.

______________________________________________
R-help@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.

Reply via email to