How can i create 2d array with list comprehesion? - julia

To make, 2d array with list comprehesion, i write this:
array = [f(i) for in 1:length]
at this, function f returns 1-d list.
But at the result as nested array....
How can i create 2d array with list comprehension?
The example of 2d dimension is like this:
julia> A
2×3 Array{Float64,2}:
0.0194681 0.195811 0.150168
0.398199 0.544672 0.942663

Since your f already returns a vector (I assume you refer to this type when you write "1-d list") then it is not possible to create a matrix using a comprehension (unless you want to write f(i)[j] in the example of Przemyslaw which will be inefficient).
What you should do is:
reduce(hcat, [f(i) for i in 1:len])
to get a matrix whose columns are the values returned by f(i).

Here it is:
julia> [x*y for x in 1:5, y in 1:3]
5×3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15

Related

Converting Julia nested list to multidimensional array

Given a Julia list of lists:
data = [[1,2],[4,5]]
which has type Vector{Int64}, how can I convert this to a 2D data type (e.g. 2×2 Matrix{Int64}) so that I can index it like data[:,2]? I tried hcat or vcat but couldn't get the result that I wanted. Thanks in advance!
You can do:
julia> reduce(hcat, data)
2×2 Matrix{Int64}:
1 4
2 5
hcat works fine:
julia> hcat([[1,2],[4,5]]...)
2×2 Matrix{Int64}:
1 4
2 5
The thing is that vectors are column-vectors in Julia (unlike in NumPy, for example), so you should horisontally concatenate them to get the matrix.
If you use vcat, you'll stack them on top of each other, getting one tall vector:
julia> vcat([[1,2],[4,5]]...)
4-element Vector{Int64}:
1
2
4
5
You can use Iterators for that. Once you have a Vector simply use reshape.
reshape( collect(Iterators.flatten([[1,2],[4,5]])), 2,2 )
2×2 Matrix{Int64}:
1 4
2 5

how to get an array from a data frame

How can I get an array form a column in data frame satisfying a condition?
example:
x=data.frame(pn=c('a','b','c','d','e','f'),price=c(1,2,3,4,5,6))
Then, for a given list of pn (an array that can have any size), like this:
y=c('a','b','f','a','a','b','b','a','f','f')
I want an array of prices regarding y. The expected output is:
1,2,6,1,1,2,2,1,6,6
(No loop or lambda function)
Use a named vector to match
unname(setNames(x$price, x$pn)[y])
#[1] 1 2 6 1 1 2 2 1 6 6

Create vector by given distibution of values

Let's say I have a vector a = (1,3,4).
I want to create new vector with integer numbers in range [1,length(a)]. But the i-th number should appear a[i] times.
For the vector a I want to get:
(1,2,2,2,3,3,3,3)
Would you explain me how to implement this operation without several messy concatenations?
You can try rep
rep(seq_along(a), a)
#[1] 1 2 2 2 3 3 3 3
data
a <- c(1,3,4)

Create block matrices with identical value in R

I want to create k blocks with identical values in a n*n matrix (k can be divided exactly by the row number times the columns number as n*n ):
for example, when n = 4 and k = 4, (k can be divided exactly by 4*4=16), a matrix is create like this:
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
How can I do this without a for loop?
There's a fantastically useful mathematical operator called a Kronecker product:
m1 <- matrix(1:4,nrow=2,byrow=TRUE)
m2 <- matrix(1,nrow=2,ncol=2)
kronecker(m1,m2)
The Matrix package has methods for Kronecker products of sparse matrices (?"kronecker-methods"), so that you can easily build huge sparse patterned matrices as long as you can find a way to express the pattern in terms of Kronecker products.

Reducing the dimensionality of a vector

How can I reduce the size of a vector to a lower dimension?
Say for example X:=(1,2,3,4,5,6,7,8,9,10) is a 10-D vector. Suppose
I want to reduce it to a 5 dimensional space. Is there any way to do this?
I have a situation where I need to compare an N-d vector with a corresponding vector of a lower dimension.
There are an infinite number of ways to convert a 10d vector into a 5d vector.
This is like saying "I want a function that takes two integer parameters and returns an integer, can I make such a function". There an infinite many such functions.
It really depends on what you want to do with the vector. What are the meanings of your 10d and 5d vectors?
If my assumption is right, the OP would like to convert a vector of 10 values to a matrix with 2 columns.
This could be done easily in R:
# make up the demo data
> v <- c(1,2,3,4,5,6,7,8,9,10)
# modify the dimensions of 'v' to have 2 columns
> dim(v) <- c(5,2)
# and check the result
> v
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10

Resources