Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop-Created Matrices in Julia

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})

like image 806
Arya Pourtabatabaie Avatar asked Nov 21 '15 21:11

Arya Pourtabatabaie


People also ask

How do you write matrices in Julia?

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).

How do you access the matrix elements in Julia?

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.

How do you reshape an array in Julia?

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.


1 Answers

Instead of two nested comprehensions, just use one multidimensional comprehension:

F = [f(ui,vi) for vi in v, ui in u]
like image 64
mbauman Avatar answered Sep 30 '22 20:09

mbauman