This question already has answers here:
given value of matrix, getting it's coordinate
(2 answers)
Closed 9 years ago.
I have matrix A, I want to get it's element coordinate (row and col) given element of that matrix.
I used which(A== number), but it doesn't give me the row and col number of given element.
does anyboday have idea which function I should use ?
> A
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
For example, for given element = 18, I want to get coordinate like : 3, 4
Use arr.ind=TRUE in which
> A <- matrix(1:20, ncol=4)
> which(A==18, arr.ind=TRUE)
row col
[1,] 3 4
Related
How to find the column of a matrix which has the maximum L2 norm? The matrix has NA values in some columns, we want to ignore those columns.
The following code I am trying, but it shows error due to NA values.
#The matrix is T
for(i in 1:ncol(T)){
if(norm(y,type='2') < norm(T[,i],type = '2'))
y = T[,i]
}
I think it would also be useful if we could somehow get the columns of T as a list, since we could use which.max function then, but I could not do that. Is that possible?
Please help
Maybe you can write your own L2 norm and find the column with the maximum, i.e.,
which.max(sqrt(colSums(T**2)))
Example
T <- matrix(c(1:10,NA,12:19,NA),nrow = 4)
> T
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 NA 15 19
[4,] 4 8 12 16 NA
> which.max(sqrt(colSums(T**2)))
[1] 4
I have large size matrix and try to find the column that has the minimum value for each row. For instance, here is my matrix, (simply generate with matrix(sample(12),nrow = 3)). With the matrix I want to to have a vector (3,4,1) representing the column number which contains the lowest value in each row.
How should I do it? It could be duplicated question but I could not find answers.
[,1] [,2] [,3] [,4]
[1,] 10 11 1 12
[2,] 8 9 7 3
[3,] 2 5 6 4
Use max.col:
max.col(-mat)
# [1] 3 4 1
Here's my problem:
I have a vector and I want to convert it into a matrix with fixed number of columns, but I don't want to replicate the vector to fill the matrix when it's necessary.
For example:
My vector has a length of 15, and I want a matrix with 4 columns.I wish to get the matrix wit 15 elements from the vector and a 0 for the last element in the matrix.
How can I do this?
Edit:
Sorry for not stating the question clearly and misguiding you guys with my example. In my program,I don't know the length of my vector, it depends on other parameters and this question involves with a loop, so I need a general solution that can solve many different cases, not just my example.
Thanks for answering.
You could subset your vector to a multiple of the number of columns (so as to include all the elements). This will add necessary amount of NA to the vector. Then convert to matrix.
x = 1:15
matrix(x[1:(4 * ceiling(length(x)/4))], ncol = 4)
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
If you want to replace NA with 0, you can do so using is.na() in another step
We can also do this with dim<- and length<-
n <- 4
n1 <- ceiling(length(x)/n)
`dim<-`(`length<-`(x, n*n1), c(n1, n))
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
data
x <- 1:15
This question already has answers here:
Get the row and column name of the minimum element of a matrix
(2 answers)
Closed 5 years ago.
I wish to find the maximum element-value of a matrix and it's location (in row and column id in the matrix).
I am using the following function to return the row and column of the matrix.
This seems like a bad hack -- it's the sort of thing where i'm probably missing a native method. Is there a better / more R way?
Here's my function:
matxMax <- function(mtx)
{
colmn <- which(mtx == max(mtx)) %/% nrow(mtx) + 1
row <- which(mtx == max(mtx)) %% nrow(mtx)
return( matrix(c(row, colmn), 1))
}
I use is as follows:
mm <- matrix(rnorm(100), 10, 10)
maxCords <- matxMax(mm)
mm[maxCords]
You could do
## Some data
set.seed(123)
mm <- matrix(rbinom(40, 20, 0.5), 8, 5)
mm
# [,1] [,2] [,3] [,4] [,5]
# [1,] 9 10 8 11 11
# [2,] 12 10 6 11 12
# [3,] 9 14 9 10 6
# [4,] 13 10 14 11 10
# [5,] 13 11 13 9 12
# [6,] 6 10 11 8 8
# [7,] 10 7 11 14 9
# [8,] 13 13 16 13 8
which(mm == max(mm), arr.ind = TRUE)
# row col
# [1,] 8 3
I am trying to cut one row
x = [1 2 3 4 5 6 7 8 9 10 11 12]
into multiple rows of equal length so that
y(row1) = [1 2 3 4
y(row2) = 5 6 7 8
y(row3) = 9 10 11 12]
I know I can achieve this using a combination of rbind and cbind, but the dataset I am trying to apply this to is much larger than the example, so I am looking for a way to do it more quickly and automatically. I tried cut and cut2 but those didnt work either
jelle
The function matrix() is your friend here:
> matrix(1:12, nrow = 3, byrow = TRUE)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
Note the optional parameter, byrow. The default is FALSE and will fill the matrix by columns, setting it to true in this case gets the data arranged in the order that you described.Just something to be careful about, since R won't throw an error if you fill by column, but your data won't be in the right format!
Use matrix:
> y <- 1:12
> y
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> matrix(y,3,4,byrow=1)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
Edit: I included the byrow=TRUE argument to matrix (pointed out by Chase in the comments) which fills the matrix along the rows instead of down the columns.