Skip to content

In which column and in which row a number is in a matrix

4 messages · Charles Novaes de Santana, Gerrit Eichner, arun

#
Dear all, Happy New Year for all of you! I hope we have an year of
essential freedom for everyone!

I am trying to manipulate a matrix in order to know in which column
and in which row a number is allocated. But, when we use the function
"which" it returns the position of the number in the "vector
representation of the matrix". For example:
[,1] [,2] [,3]
[1,]    2    4    6
[2,]    3    5    7
[,1] [,2]
[1,]    2    5
[2,]    3    6
[3,]    4    7
[1] 3
[1] 3

The function "which" returns the position "3" for both, mata and matb,
but If I didn't know that mata is a 2x3 matrix and matb is a 3x2, I
wouldn't know that the number 4 is at the column 2 and row 1 of the
mata and at the column 1 and row 3 of the matb. Do you know any way to
know the column and the row of a number in a matrix automatically?

Thanks, best regards,

Charles
#
Hi, Charles,

see

?which

and learn about the argument arr.ind.

  Happy new year  --  Gerrit
On Wed, 2 Jan 2013, Charles Novaes de Santana wrote:

            
#
Ups... sorry, guys! Basic mistake: I forgot to look for the
information in the help of the function.

Thank you, Gerrit, for the information! All the best!

Charles

On Wed, Jan 2, 2013 at 12:21 PM, Gerrit Eichner
<Gerrit.Eichner at math.uni-giessen.de> wrote:

  
    
#
HI,
Try this:
which(mata==4,arr.ind=TRUE)
#???? row col
#[1,]?? 1?? 2
?which(matb==4,arr.ind=TRUE)
#???? row col
#[1,]?? 3?? 1
A.K.




----- Original Message -----
From: Charles Novaes de Santana <charles.santana at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, January 2, 2013 6:12 AM
Subject: [R] In which column and in which row a number is in a matrix

Dear all, Happy New Year for all of you! I hope we have an year of
essential freedom for everyone!

I am trying to manipulate a matrix in order to know in which column
and in which row a number is allocated. But, when we use the function
"which" it returns the position of the number in the "vector
representation of the matrix". For example:
? ?  [,1] [,2] [,3]
[1,]? ? 2? ? 4? ? 6
[2,]? ? 3? ? 5? ? 7
? ?  [,1] [,2]
[1,]? ? 2? ? 5
[2,]? ? 3? ? 6
[3,]? ? 4? ? 7
[1] 3
[1] 3

The function "which" returns the position "3" for both, mata and matb,
but If I didn't know that mata is a 2x3 matrix and matb is a 3x2, I
wouldn't know that the number 4 is at the column 2 and row 1 of the
mata and at the column 1 and row 3 of the matb. Do you know any way to
know the column and the row of a number in a matrix automatically?

Thanks, best regards,

Charles