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

This is similar to this question:

Add a row to a matrix in Julia?

But now I want to grow the matrix inside a function:

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

Now, outside this function:

mat = [2 3 4] f(mat)

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

Is it possible to do what I want?


Multi-dimensional arrays cannot have their size changed. There are pointer hacks to share data, but these do not modify the size of the original array.


Even if it were possible, be aware that because Julia matrices are column major, this operation is very slow, and requires a copy of the array.

In Julia, operations that modify the data passed in (i.e., performing computations on data instead of with data) are typically marked with !. This denotes to the programmer that the collection being processed will be modified. These kinds of operations are typically called "in-place" operations, because although they are harder to use and reason about, they avoid using additional memory, and can usually complete faster.

There is no way to avoid a copy for this operation because of how matrices are stored in memory. So there is not much real benefit to turning this particular operation into an in-place operation. Therefore, I recommend against it.


If you really need this operation for some reason, you should not use a matrix, but rather a vector of vectors:

v = Vector{Float64}[] push!(v, [1.0, 2.0, 3.0])

This data structure is slightly slower to access, but <strong>much</strong> faster to add to.

On the other hand, from what it sounds like, you may be interested in a more specialized data structure, such as a DataFrame.


I agree with Fengyang's very comprehensive answer and excellent avoidance of an XY Problem. Just adding my 2¢.

push!ing is a fairly expensive operation, since at each push a new location in memory needs to be found to accommodate the new variable of larger size.

If you care about efficiency, then it would be a lot more prudent to preallocate your mat and mutate it (i.e. alter its contents) inside your function, which is permitted, i.e.

julia> mat = Array(Int64, (100,3)); # mat[1,:] contains garbage values julia> f(mat, ind, x) = mat[ind,:] = x; julia> f(mat, 1, [1 2 3]); # mat[1,:] now contains [1,2,3]

If the reason you prefer the push! approach is because you don't want to keep track of the index and pass it manually, then you can automate this process in your function too, by keeping a mutatable counter e.g.

function f(mat, c, x) c[1] = c[1] + 1; mat[c[1], :] = x; end;

julia> mat = Array(Int64, (100,3)); counter = [0]; julia> f(mat, counter, [1 2 3]); julia> f(mat, counter, [1 2 3]); julia> f(mat, counter, [1 2 3]); julia> mat[1:3,:] 3×3 Array{Int64,2}: 1 2 3 1 2 3 1 2 3

Alternatively, you can even create a closure, i.e. a function with state, that has an internal counter, and forget about keeping an external counter variable, e.g.

julia> f = () -> (); # creating an f at 'outer' scope julia> let c = 1 # creates c at local 'let' scope f = (mat, x) -> (mat[c,:] = x; c += 1;) # f reassigned from outer scope end; # f now contains the 'closed' variable c julia> mat = Array(Int64, (100,3)); julia> f(mat, [1 2 3]); julia> f(mat, [2 3 4]); julia> f(mat, [3 4 5]); julia> mat[1:3,:] 3×3 Array{Int64,2}: 1 2 3 2 3 4 3 4 5


  • ADAL for Windows Store SSO
  • Jquery hide first 12 elementes, show next 12 elements Previous and Next
  • Error: cannot access android.app.Activity
  • How is almost everything in Javascript an object?
  • Qmake: how to check for Qmake version
  • Whether performance will impact when database procedure is called from application many times?
  • Xcode 7.2 & Instruments memory leak not working for c++ code
  • std random functions not working - Qt MinGw
  • How can I stop ASP.net from marking .dll's read only then throwing errors on rebuild?
  • Javascript click a class button
  • Katalon Studio creating element with webDriver not recognised when used in Javascript
  • QtCreator: How to compile external source files
  • PHP - Prepend a url to all links with no http or https
  • Maximum number of workers in a parallel loop (local cluster) with Matlab R2014a?
  • How Can I Package a Unity3D 5 Windows Application into a single exe file?
  • Where to obtain qmake (QT) compiler for netbeans?
  • How can I set my enum with the value the user enter in a EditText?
  • Issues building PyQt5 for Python3.4 in Ubuntu 14.04 caused by qprinter.h not found then pyuic5 error
  • Android studio fail to install app on device
  • How to solve mingw32-make.exe exited with code 2 error?
  • Changing a fixed website to being fluid (CSS)
  • Error: Unable to activate Windows Store app …. The xxx.exe process started, but the activation reque
  • Can't push to existing live heroku app on Cedar Stack
  • Conda install pygmaps
  • Problems with Magento prices after included tax class to product
  • which method is equivalent Rand (int) in Visual Fox Pro to C #
  • run Qt app with dependent libraries on mac with no admin rights
  • Visual Studio WDK integration Issue
  • Android Studio 3.0 error: android-apt is incompatible
  • Voice recording applet/flash in a webbrowser
  • Program doesn't stop after returning 0 from main
  • Qt Linguist - set translator for application
  • Pycharm Linux. How to change color scheme of selected file in Project Inspetor [closed]
  • Cross-thread cross-form. Display a splash screen with a progress bar
  • Right clicking suddenly unbearably slow in VS 2010
  • Redirecting from Global.asax in Medium Trust
  • Regex to redirect a url using url rewrite
  • Count & print the checked inputs with AngularJS
  • Google Play services out of date. Requires 9877000 but found 9875480 with google firebase
  • Cannot start Mysql on Mac
  • Block access to a web subdirectory with .htaccess
  • Dependent menus in HTML and JavaScript
  • Memory map in IDA Pro similar to OllyDbg
  • alert() and console.log() not working in Firefox 26
  • MAMP Pro does not log PHP errors
  • Laravel Schema Builder alter storage engine
  • Android - package com.google.android.youtube.player does not exist
  • Incrementing the CSS padding-top property in Javascript
  • Pygame on mac error with loading images
  • fonts.googleapis.com wants on local
  • Entity Framework Migrations - Enable AutoMigrations along with added migration
  • Storing checkbox values in sessions and then re-selecting check boxes checked when they came back to
  • Can not use class org.json.XML on Android Studio
  • Segmentation fault when I run rails S (cant compile nokogiri)
  • Java card weird response to long AID and Short AID applets
  • ImportError: No module named app_name
  • WebLogic 10.5.3 + Seam 2.2.1: Application does not start
  • Use of variables like %{buildDir} in QtCreator kit settings in Qt5
  • cmake - get the used commandline flags “-D”
  • Failed to resolve 'com.google.firebase:firebase-messaging:11.0.4' in Android Studio 2.3.3
  • Java.lang.NoClassDefFounderError: Could not initialize class groovy.net.http.ParserRegistry
  • Failed to resolve variable '${animal.sniffer.version}' when migrate to AndroidX
  • What is the best way to handle uploaded text files of different encodings?
  • Uninstalling MAMP PRO from my mac
  • Minimizing browser window in Firefox & Chrome adds white space to right of page
  • Adding google font api to select menu
  • how does a SQL query (Pro*C) be able to find set of values that are not present in a db table
  • Python newb MacOS in IDLE and PyCharm ModuleNotFoundError: No module named 'nltk'
  • Android Studio can't install because it can't find jdk
  • Upload file in Android with outofmemory error
  • VM has multidex support, MultiDex support library is disabled
  • Android studio error when add java8 support
  • Visual Studio 2013 and Windows Phone
  • How can I tell Qt where it should look for openssl?
  • Build.Gradle issues when using Facebook SDK 4.0.0 and Parse.com libraries in Android
  • systemevents.sessionended is not being caught or fired
  • Windows 10 emulator not working in visual studio 2015
  • Visual Studio 2005 Professional Edition - Error 1311, source file not found (Yet it exists!)
  • Creating a C# Portable Class Library using command line csc.exe?
  • Material designing not supporting in android 4 and below versions
  • What are pre-requisites for learning & understanding Git?
  • programmatically access macbook pro camera java
  • Continuous Delivery Scenario - implementing Rollback
  • Cannot get types by custom attributes across assemblies
  • How to install MySQL c++ Driver on Windows [closed]
  • Generating an EDMX from a DB2 Database
  • Visual Studio Team Services: Agents Configuration for different environments in release definition
  • Qt png images not showing up in deployment app
  • Qt - LNK2019 when using function of a namespace in member function of a class
  • Getting Error while running code, I tried MultidexEnable but didnt work
  • JAVA NIO Bytebuffer.allocateDirect() size limit over the int
  • Android GLES20 works on bionic 4.1.2 but not on Samsung 4.4.2
  • Import large sql file django
  • Fonts and Font Awesome icons not loading over SSL
  • Qt 5.2.1 for Android on Windows not building
  • Win32 API timers
  • The requested operation requires elevation
  • Warning: Error: dyld: lazy symbol binding failed: Symbol not found: _objc_autoreleasePoolPush
  • Get valid URL from string using Bash script
  • How to login to a JSP webpage using Android?
  • Representing a couple of RDF-triples using tensor. How to programming this modeling process using Py
  • select2 is not a function, wordpress
  • Gradle project sync failed after Android-Studio (3.1) update
  • How to change progressbar value when async task running
  • Wordpress cannot connect to mysql server
  • How to generate entity classes from nhibernate mapping files during runtime
  • Android Studio Failed to resolve: multidex Open File
  • Vimeo Video Player in HTML5
  • Html Helpers Help [closed]
  • Neo4j Java 7 Terminal Issue
  • Trouble with initial distribution of C# .NET application
  • com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed on android 3.2.
  • Difference (if there is any) between ` and ' in javascript [duplicate]
  • Where do i find System.Windows.Media ? The Media is not exist
  • How to deserialize Xml file with nested elements of same name?
  • How to ignore http link in string and return everything else?
  • how to consult file as a module in jpl
  • While scanf EOF loop misbehaving
  • Difference between heading inside section or before it in HTML5
  • Spring Books: Which one to choose [closed]
  • ASP.NET MVC: Restricting access using url
  • where is blend for visual studio 2012
  • Xml Schemas and Date Times
  • Cannot find namespace “ServiceModel” with Unity
  • Xml Schemas and Date Times
  • Convert pdf to Word document [closed]
  • How can I install Application Request Routing ARR 3.0 in Windows 10 Preview?
  • No rails commands will run
  • NumPy 64bit fail to install with pip on Windows with Python 64bit
  • How does one change an instruction with a hex editor?
  • qmake .pro file could not be parsed
  • Unlock android mobile with PIN code programmatically Selenium mobile automation
  • SSRS Report is 'dropping' rows.
  • How to Clean Up subprocess.Popen Instances Upon Process Termination
  • Why does setting frameInterval on a SKView not work?
  • How to add module In Android studio
  • py2neo - Neo4j - System Error - Create Batch Nodes/Relationships
  • qmake gives code 3 when attempting to configure qt for static building
  • why do we “pack” the sequences in pytorch?
  • New to Bash. Can't pass arguments to the bash script
  • How to build & store this large lower triangular matrix for matrix-vector multiplication?
  • Google Analytics / campaign measurement
  • AS3 override addChild(), test for type
  • Differences in simulator vs deployment on iPad
  • How to generate 64-bit Visual Studio projects from qmake
  • How do I create a build for a legacy system?
  • Tab through XML documentation fields
  • Is there a way of restricting an API's processor resource in c#?
  • Django ManyToMany filtering by set size or member in the set
  • Submit javascript dynamically added elements to controller method like Stackoverflow
  • Getting “Sorry! The configured backend (pro) does not know about ionic upload” When trying to upload
  • ORMLite dataType=DataType.SERIALIZABLE doesn't work
  • subset array with Fortran given a condition?
  • Set LayoutParams on parentless LinearLayout
  • grails mongodb connection refused
  • IIS - Missing Windows Authentication Feature [closed]
  • java.util.zip.ZipException: duplicate entry: AbstractHttpContent.class
  • How to import a BIG SQL file into a rails database? [duplicate]
  • Language for web scraping JAVASCRIPT content
  • failed to instantiate one or more class in android studio
  • Mediafire developer doesn't show me Create new app
  • problems with 'com.android.support:appcompat-v7.27.1.1' in the gradle file
  • A larger heap for the Gradle daemon is recommended for running jack
  • :app:transformClassesWithMultidexlistForDebug FAILED
  • PrimeFaces input components are not highlighted on validation error
  • get_html_translation_table expects at most 2 parameters, 3 given
  • Copying timestamp columns within a Postgres table
  • How to add some text in google piechart pieHole
  • Value in dropdown menu (Removed)
  • Logic in property getters [closed]
  • Owncloud upload file to specific folder - curl
  • Could not find the gcm.jar error when creating App Engine backend => suggests installing deprecat
  • Listening to a polymer attached event from outside that element
  • How do I improve Rails / Paperclip image compression inside or ImageMagick / Rmagick?
  • C++ 11 std::thread strange behavior
  • Including Python.h in Qt application causes undefined reference to Qt functions
  • Android studio and eclipse + ADT
  • Can comments make any difference during the run-time?
  • Web API Basic Auth inside an MVC app with Identity Auth
  • numberOfRowsInSection called but cellForRowAtIndexPath not called
  • Eclipse GUI building [duplicate]
  • Why does the address of an object change across methods?
  • Is there ever a reason to use is versus as? [duplicate]
  • Client-side prediction & server reconciliation
  • Division in C# to get exact value [duplicate]
  • Serial communication over USB converter in Python - how to aproach this?
  • what does the follow prolog codes do?
  • Disable check for override in gcc
  • Hadoop shuffle uses which protocol?
  • SQL Stored Procedure - variable too short
  • What are zone turns?
  • Proguard Exception java.io.IOException: Duplicate zip entry
  • what is the correct way to use coffeescript with ember´s Mixins?
  • Is there an HTML code that can make my background picture transparent and my text non-transparent?
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • iOS - Is this a task for enums?
  • Subversion reverting resolved file
  • DataGridView: Pass by Value or Reference?
  • FTS3 searches in ORMLite?
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • Where these are stored?
  • JSON encode and decode on PHP
  • abstracting over a collection
  • How can I tell a form not to dispose a particular control when it closes?
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • Fail:(TESTMODE) Transactions of this market type cannot be processed on this system
  • Compiling dlib on OS X
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Authentication in Play! and RestEasy
  • How to assign byte[] as a pointer in C#
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Do I need to reset a Perl hash index?
  • Control modification in presentation layer
  • javascript inside java/jsp code
  • Sending data from AppleScript to FileMaker records
  • When should I choose bucket sort over other sorting algorithms?
  • retrieve vertices with no linked edge in arangodb