Index for array that matches vectorwise - r

I have an array
h <- array(0, c(100, 5, 9));
The length 9 vector is sort of an identification sequence. Given some i in 1:100, is there a way I can obtain the index j in 1:5 for which the length 9 sequence matches some vector V? ie. the index j such that
identical(h(i,j,1:9),V)
is true.
Edit: this is assuming that one of the 5 do match for the given i. It'd be even better if one can obtain the pair (i,j) for which it matches with V.

Related

Is there a way to access an element in amultidimensional array using a variable to store the indexes

Let's say I have a matrix A with n rows and m columns. If I want to acess the element on the i'th row and j'th column I can access it as A[i,j].
I would like to know how I can save this i,j in a variable x so that I can get A[x] = A[i,j]. storing them in a vector won't help since A[i,j] != A[c(i,j)].
I would like to be able to do this for a higher dimensional array where I don't know the dimensions ahead of time because they depend on the data that is suploaded by the user.
thank you very much!
Edit
what I ended up doing:
If x is the vector representing the Indices I needed, the solution is A[matrix(x,nrow=1))
You can use cbind or calculate the position.
A <- matrix(1:12, 3)
A[2,3]
#8
A[3,1]
#3
i <- c(2,3)
j <- c(3,1)
x <- cbind(i,j)
A[x]
#[1] 8 3
x <- i + (j-1)*dim(A)[1]
A[x]
#[1] 8 3

R: Accessing elements of 2D matrix with vectors of indices

Suppose I have a 3 X 15 matrix "phi", and I want to create a vector of entries from phi corresponding to an i,j combination, where i is a length 900 vector of numbers 1:3, and j is a length 900 vector of numbers 1:15. In other words, I want a length 900 vector of phi values, where the first element is phi[i[1], j[1]], the second element would be phi[i[2], j[2]], etc.
My initial thought was phi_list <- phi[i, j], but that appears to give back every combination of i,j values. So, how would I go about constructing such a vector?
Thanks for any help!
In this case, we can use the index as a matrix with the i for row index and 'j' for column index
phi[cbind(i, j)]
#[1] 6 18 35
If we use the i and jvectors in the 'i', and 'j' it would return a matrix by including the rows and columns included in the index instead of picking the elements that matches the location
data
set.seed(24)
phi <- matrix(1:50, 5, 10)
i <- c(1, 3, 5)
j <- c(2, 4, 7)

How do I match one vectors index positions to a different vectors index positions?

I have two vectors with different values. I have sorted the second vector and need to rearrange the first vector so that it matches the index positions of the second vector. For example if vector B has values 3, 5, 1, 2 rearranged to 1,2,3,5, I need to sort vector A so that the index positions are the same as the positions of vector B rearranged. I've tried:
>sort(VectorB)
>match(c[VectorA], c[sort(VectorB)]
You are looking for VectorA[order(VectorB)]. To understand this issue, try
sig <- order(VectorB)
VectorB[sig]
VectorA[sig]

Convert a one column matrix to n x c matrix

I have a (nxc+n+c) by 1 matrix. And I want to deselect the last n+c rows and convert the rest into a nxc matrix. Below is what I've tried, but it returns a matrix with every element the same in one row. I'm not sure why is this. Could someone help me out please?
tmp=x[1:n*c,]
Membership <- matrix(tmp, nrow=n, ncol=c)
You have a vector x of length n*c + n + c, when you do the extract, you put a comma in your code.
You should do tmp=x[1:(n*c)].
Notice the importance of parenthesis, since if you do tmp=x[1:n*c], it will take the range from 1 to n, multiply it by c - giving a new range and then extract based on this new range.
For example, you want to avoid:
(1:100)[1:5*5]
[1] 5 10 15 20 25
You can also do without messing up your head with indexing:
matrix(head(x, n*c), ncol=c)

How to create a list from an array of z-scores in R?

I have an array of z-scores that is structured like num [1:27, 1:11, 1:467], so there are 467 entries with 27 rows and 11 columns. Is there a way that I can make a list from this array? For example a list of entries which contain a z-score over 2.0 (not just a list of z scores, a list which identifies which 1:467 entries have z > 2).
Say that your array is called z in your R session. The function you are looking for is which with the argument arr.ind set to TRUE.
m <- which(z > 2, arr.ind=TRUE)
This will give you a selection matrix, i.e. a matrix with three columns, each line corresponding to an entry with a Z-score greater than 2. To know the number of Z-scores greater than 2 you can do
nrow(m)
# Note that 'sum(z > 2)' is easier.
and to get the values
z[m]
# Note that 'z[z > 2]' is easier

Resources