How to create multidimensional space of points in R - r

I created one dimensional array of random numbers between 0 and 1 using
myData <- runif(1000, 0.0, 1.0);
How can I create an n dimensional array of 1000 nodes. for example a 10 dimensional random points

The function array() will create an array of arbitrary number of dimensions. The dim argument specifies the dimensions. So, to create an array of 1000 points in a 10x10x10 array, we use
a <- array(runif(1000), dim=c(10,10,10))
This can be extrapolated to any number of dimensions you wish.
For example
a <- array(runif(1000), dim=c(2,2,2,5,5,5))
creates a 6 dimensional array with 1000 (=2*2*2*5*5*5) points. There is no way to decompose 1000 points into a 10 dimensional array, unless some of the dimensions have length 1.
To access values of the array, you can use standard subsetting with [ ], and specify the correct number of dimensions within the brackets. E.g.
a[1,2,1,5,3,2]
# [1] 0.3232738
It is worth noting that a matrix in R is simply a special case of an array with two dimensions.

Related

Julia: Turn Vector into multiple m x n matrices without a loop

Let's say I have a vector V, and I want to either turn this vector into multiple m x n matrices, or get multiple m x n matrices from this Vector V.
For the most basic example: Turn V = collect(1:75) into 3 5x5 matrices.
As far as I am aware this can be done by first using reshape reshape(V, 5, :) and then looping through it. Is there a better way in Julia without using a loop?
If possible, a solution that can easily change between row-major and column-major results is preferrable.
TL:DR
m, n, n_matrices = 4, 2, 5
V = collect(1:m*n*n_matrices)
V = reshape(V, m, n, :)
V = permutedims(V, [2,1,3])
display(V)
From my limited knowledge about Julia:
When doing V = collect(1:m*n), you initialize a contiguous array in memory. From V you wish to create a container of m by n matrices. You can achieve this by doing reshape(V, m, n, :), then you can access the first matrix with V[:,:,1]. The "container" in this case is just another array (thus you have a three dimensional array), which in this case we interpret as "an array of matrices" (but you could also interpret it as a box). You can then transpose every matrix in your array by swapping the first two dimensions like this: permutedims(V, [2,1,3]).
How this works
From what I understand; n-dimensional arrays in Julia are contiguous arrays in memory when you don't do any "skipping" (e.g. V[1:2:end]). For example the 2 x 4 matrix A:
1 3 5 7
2 4 6 8
is in memory just 1 2 3 4 5 6 7 8. You simply interpret the data in a specific way, where the first two numbers makes up the first column, then the second two numbers makes the next column so on so forth. The reshape function simply specifies how you want to interpret the data in memory. So if we did reshape(A, 4, 2) we basically interpret the numbers in memory as "the first four values makes the first column, the second four values makes the second column", and we would get:
1 5
2 6
3 7
4 8
We are basically doing the same thing here, but with an extra dimension.
From my observations it also seems to be that permutedims in this case reallocates memory. Also, feel free to correct me if I am wrong.
Old answer:
I don't know much about Julia, but in Python using NumPy I would have done something like this:
reshape(V, :, m, n)
EDIT: As #BatWannaBe states, the result is technically one array (but three dimensional). You can always interpret a three dimensional array as a container of 2D arrays, which from my understanding is what you ask for.

How can I replicate an array multiple times to create a 3d array in R?

I have an array (mask) that I am using as a Land Sea mask in R of size 189x420.
I want to replicate this array to the 3-dimensional size of my dataset (let's call it sample_data) so that I can multiply one by the other to apply a land mask to my sample_data using the command below:
masked_data<- gridArithmetics(sample_data, mask, operator = "+")
If my sample_data is 60x189x420, I want to replicate my Land Sea Mask 60 times and create a new array of size 60x189x420.
I have tried:
x<-replicate(mask,5)
But this returns a 3d array of size 189x420x60. If I use aperm to try and rotate/transpose it, I end up with an array of size 60x420x189, which is not useful as this is a Land mask and I don't want to transpose the data within each individual array.
How can I replicate my mask array to create a 3d array of size 60x189x420?
As an follow-on/alternative, is there any sort of dot notation in r that could be used to multiply one array (mask) (189x420) by 3-dimensional data (sample_data of size 60x189x420) so that the land mask is multiplied by each individual array within the sample data (so multiplication occurs 60 times to create a new (masked) array the same size as sample_data)?

how to repeat a function using different multiple input matrices

I want to use the function "nestedness(M)" from the "bipartite" R package. It calculates an index from a matrix (M). I have an array with 1000 matrices and I want to apply this function 1000 times varying the input matrix file each time. I have tried apply family functions but the solution does not come this way. I don't know how to vary the input of a function when it is not a number but a matrix. Any aid to put me on the way would be very very appreciated.
Lets say you have an array that is 3x3x3 ie 3 matrices that are each 3
rows and 3 columns. The dimensions of an array are c("row", "column",
"slice"). You can use apply over any of these dimensions. In your
case over the 3rd dimension will calculate your function over each
matrix. Here is the example array:
a <- array(1:27, dim = c(3,3,3))
Now calculate the max function for each slice (dimension 3) of the array
apply(a, 3, max)
[1] 9 18 27

Julia distribute function: specifying distributed dimension

I'm interested in distributing an MxN integer array across p workers. Is there a way to specify which dimension gets distributed? In particular, I want to keep the number of rows M fixed and distribute over N columns. In my case M > N (I have a term-document matrix with vocabulary of size M and number of documents N).
By default, Julia appears to distribute over the dimension that has the largest size, which doesn't work for my application (I want to distribute over the documents and not the vocabulary). Is there a way to control which dimension gets distributed?
SharedArray constructor has a pids optional parameter which maps elements to processes (see documentation).
So, an MxN matrix can be initialized with the following code:
# a helper function which might be useful in other contexts
function balancedfill(v,n,b)
d,r = divrem(n,b)
return v[[repeat(1:r,inner=d+1);repeat(r+1:b,inner=d)]]
end
# N,M = size(mat)
pidvec = repeat(balancedfill(1:nprocs(),N,nprocs()),inner=M)
sharedmat = SharedArray{Float64}((N,M); pids=pidvec)
This creates a Float64 shared array, with columns balanced between processes. Float64 can be replaced by the element-type needed. With a little change (switching inner with outer and N with M in pidvec creation) a row-wise distributed array can be created.

aperm function not clear

So I'm trying to figure out what this function aperm() does. When I do aperm(a), where a is a matrix, I get its transpose; I get that. But what about this function:
aperm(a,c(3,1:2))
what does this do? when a is a 6*7 matrix this command doesn't work.
I dont understand the R example either.
You receive the error because you are telling aperm to permute a 3 dimensional array, but only providing it a 2 dimensional array (a matrix). You need a 3 dimensional array for your command to work:
Consider the following example:
myarray <- array( 1:24, dim=c(2,3,4),
dimnames=list(One=c('a','b'), Two=c('A','B','C'), Three=1:4) )
myarray
aperm(myarray, c(3,1,2))
This creates a 3 dimensional array with dimnames to help make it clearer, then permutes it. Notice the new order of the values.
Also this example:
> myarray[2,3,4]
[1] 24
>
> mynewarray <- aperm(myarray, c(3,1,2) )
> mynewarray[4,2,3]
[1] 24
>
To get the element with value 24 in the original array we take the 2nd row of the 3rd column of the 4th layer (or whatever you want to call the 3rd dimension).
But in the permuted array it is now the 4th row (that used to be layer) of the 2nd column (which used to be rows) of the 3rd layer (that used to be columns).

Resources