I have a list of , say, 10 10x5-matrices and would like to concatenate all of them vertically to obtain a 100x5-matrix
list_of_matrices = [randn(10, 5) for _ in 1:10] # Each matrix is 10x5
output = vcat(list_of_matrices) # Would like output to be 100 x 5 dimensional
vcat and cat don't seem to work and produce a vector of matrices. What can I do?
There are two methods that work:
vcat(list_of_matrices...) can be found here
reduce(vcat, list_of_matrices)
Related
If I want to do something on each pair of letters, it could look like this in Julia:
for l1 in 'a':'z'
for l2 in 'a':'z'
w = l1*l2
# ... do something with w ...
end
end
I want to generalise this to words of any length, given a value n specifying the number of letters desired. How do I best do this in Julia?
You can use:
for ls in Iterators.product(fill('a':'z', n)...))
w = join(ls)
# ... do something with w ...
end
In particular if you wanted to collect them in an array you could write:
join.(Iterators.product(fill('a':'z', n)...))
or flatten it to a vector
vec(join.(Iterators.product(fill('a':'z', n)...)))
Note, however, that in most cases this will not be needed and for larger n it is better not to materialize the output but just iterate over it as suggested above.
Example of My Data
I have three matrix csv data files that I need to flatten and combine in R, so that I have three columns (Lat, Long, Data). The code I have for this is in matlab, but I need to convert this to R. Any thoughts? This is the matlab code that does this:
LON=csvread(‘LONGITUDE.csv’);
LAT=csvread(‘LATITUDE.csv’);
SM=csvread(‘soil_moisture20151008.csv’);
xyz=zeros(101*210,3);
k=0;
for i=1:101
for j=1:210
k=k+1;
xyz(k,1)=LAT(i,j);
xyz(k,2)=LON(i,j);
xyz(k,3)=SM(i,j);
end
end
csvwrite(‘xyz.csv’,xyz);
So far this is how I have changed it in R:
LON<-read.csv("LONGITUDE.csv", header = T)
LAT<-read.csv("LATITUDE.csv", header = T)
ET<-read.csv("actual_ET20100101.csv")
xyz=matrix(3,101,210)
k=0
for (i in 1:101){
for (j in 1:210){
k=k+1
xyz[k,1]=LAT[i,j]
xyz[k,2]=LON[i,j]
xyz[k,3]=ET[i,j]
}
}
write.csv("xyz.csv",xyz);
I'm not sure what I'm doing wrong. Any guidance on this issue would be greatly appreciated.
Finally, I have a whole directory of files that I need to run this script on, so any ideas on how to apply this to a directory would be great. The LAT/LON files don't change, just the data files.
Thank you!!
If I am understanding your data correctly, you have a large number of matrix files, where each index (row/column position) is assigned to the same data value. That is, (1,1) in each matrix gives the value of interest for the 1st data point, and (1,2) gives values for a different data point.
In that case, you should just be able to convert them all to a matrix, extract the values as a vector, then stitch them together.
To illustrate, here are three identical data.frames (so that we can see if they align correctly:
A <- B <- C <-
data.frame(matrix(runif(36), nrow = 6))
Each data.frame is this:
X1 X2 X3 X4 X5 X6
1 0.2462450 0.6887587 0.216578122 0.5982332 0.2402868 0.9588999
2 0.5924075 0.7511237 0.813704807 0.6892747 0.6253069 0.4648226
3 0.7482773 0.4808986 0.006036452 0.6576487 0.5752148 0.5554258
4 0.8545323 0.6822942 0.654128179 0.6582181 0.8173544 0.5191778
5 0.1748737 0.7456279 0.992209169 0.4468014 0.3491022 0.9736064
6 0.7189847 0.3424291 0.581840006 0.1460138 0.8071445 0.2920479
Then, I put them all in a list (named, so that the columns come out named):
myList <- list(A = A, B = B, C = C)
Then, we loop through the list, converting each data.frame to a matrix, then extracting the values as a vector. Then, I convert the resulting list to a data.frame to get the column/row behavior you likely want (data.frames are just lists with special properties; each column is an element of the list, but data.frames assumes the value orders match). Note that I am using magrittr/dplyr piping to simplify the nesting in the code:
flattened <-
lapply(myList, function(x){
as.matrix(x) %>%
as.numeric()
}) %>%
as.data.frame()
Then, the head of this (from my randomization) looks like:
A B C
1 0.2462450 0.2462450 0.2462450
2 0.5924075 0.5924075 0.5924075
3 0.7482773 0.7482773 0.7482773
4 0.8545323 0.8545323 0.8545323
5 0.1748737 0.1748737 0.1748737
6 0.7189847 0.7189847 0.7189847
Of note, you mentioned that you may have multiple data sources that you want to merge -- as long as you load them all up into this list, the approach will generate a column for each.
I need to apply a function that takes two arguments on matrices.
mapply(function(x, y) x+y, rbind(1:3, 1:3), rbind(2:4, 2:4))
output is
[1] 3 3 5 5 7 7
which doesn't give me the desired format I want. I need it to retain its matrix form.
On the other hand, apply function in R has an argument margin which helps retain the matrix format but only applies to one argument.
apply(rbind(1:3,1:3), MARGIN = c(1,2), function(x) x+3)
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 4 5 6
The point is there's a MARGIN argument for apply and not something like it for mapply or is there ?
PLEASE: I don't require an answer to rearrange the result, I can do it. I am using this piece of code to write a function that takes a three dimensional meshgrid which will be hassle to rearrange.
EDITED LATER:
I am really sorry, I didn't elaborate this,
Of course, I am not stuck because I wanna do
rbind(1:3, 1:3) + rbind(2:4, 2:4)
These rbinds are just examples of the vectors I am using. And the function(x, y) x+y is also an example of very long nested functions that I can't just copy here which will be so confusing and inefficient. But it is a function of two variables which is relevant for now.
I have a vector x of 12 elements. I want to create a sub vector of 4 elements which include the first 4 elements of x.
What did you read? What code did you try? help matrix leads directly to help on matrix extraction. Consider the results of code like
mat foo = J(12,1,1)
mat foo4 = foo[1..4, 1]
mat li foo4
I am working with Mata (Stata) trying to append (vertically concatenate) many vectors.
I would like to do something like
mat C = A\B
but since I have about 300 column vectors to append in a unique column vector, I would like to know if there is a command to do it (since it is quite difficult to type
mat C = c1\c2\c3...
300 times).
The code you cite is Stata's matrix language, which is not Mata.
How did you get these vectors in the first place? Are they named systematically?
There is a likely to be an easy answer depending on the details.
For example, in Stata you can go with column vectors c1 ... c300
mat C = c1
forval j = 2/300 {
mat C = C \ c`j'
}
although the matsize limit may mean you are better off handling such a column vector as a Stata variable or in Mata.
EDIT: To produce a matrix in Stata from those columns, use , not \.
This code adopts Nick's logic but uses the Mata language.
mat c=(1,4,7,10\2,5,8,11\3,6,9,12) // 3 x 4 matrix
mat list c
mata
c=st_matrix("c") // Stata matrix into Mata matrix
x=c[.,1]
for (i=2; i<5;i++) {
x=x\c[.,i]
}
st_matrix("newC",x) // Mata matrix into Stata matrix
end
mat list newC