Add a row to a matrix inside a function (and propagate the changes outside) in Julia? -


this similar question:

add row matrix in julia?

but want grow matrix inside function:

function f(mat)     mat = vcat(mat, [1 2 3]) end 

now, outside function:

mat = [2 3 4] f(mat) 

but doesn't work. changes made mat inside f aren't propagated outside, because new mat created inside f (see http://docs.julialang.org/en/release-0.4/manual/faq/#functions).

is possible want?

multi-dimensional arrays cannot have size changed. there pointer hacks share data, these not modify size of original array.


even if possible, aware because julia matrices column major, operation slow, , requires copy of array.

in julia, operations modify data passed in (i.e., performing computations on data instead of with data) typically marked !. denotes programmer collection being processed modified. these kinds of operations typically called "in-place" operations, because although harder use , reason about, avoid using additional memory, , can complete faster.

there no way avoid copy operation because of how matrices stored in memory. there not real benefit turning particular operation in-place operation. therefore, recommend against it.


if really need operation reason, should not use matrix, rather vector of vectors:

v = vector{float64}[] push!(v, [1.0, 2.0, 3.0]) 

this data structure slower access, much faster add to.

on other hand, sounds like, may interested in more specialized data structure, such dataframe.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -