Let's say we have arrays u and v, and function f. We want a matrix F consisting of f(ui, vi) for all members of u and v. Trying this:
F = [ [f(ui,vi) for vi in v] for ui in u]
The result is an array of arrays (In Julia's words, Array{Array{Int64,1},1}
)
How can I reshape this into a 2-dimensional array? (Array{Int64,2}
)
Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A).
A 2D array in Julia is known as a Matrix. Elements in Matrix are accessed with the use of both row and column index. A 2D array can be created by removing the commas between the elements from a Vector at the time of array creation.
Reshaping array dimensions in Julia | Array reshape() Method The reshape() is an inbuilt function in julia which is used to return an array with the same data as the specified array, but with different specified dimension sizes. Parameters: A: Specified array.
Instead of two nested comprehensions, just use one multidimensional comprehension:
F = [f(ui,vi) for vi in v, ui in u]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With