##### Profile
Join date: May 15, 2022 Dec 12, 2012 hindi Q: How to conditionally add a new row to a matrix in Julia? I'm working on a function in Julia that takes a matrix and adds a new row to it. The matrix has to be 4x4, so it's basically a simple horizontal concatenation. It's not really an add-on. So far, my approach is to: Get the data: >> mymatrix = Matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) Get the dimensions of the matrix: >> mymatrix.size (4, 4) Get the dimensions of the new row: >> mynewrow.size (4, 1) Concatenate the matrices: >> mymatrix + mynewrow (4×4) Array{Int64,2}: 1 2 3 4 5 6 7 8 Which is great, but I'd like to add some conditional logic to this, so that the new row is only added if the matrix is 4x4. This is my current approach: function add_to_matrix(m::Matrix, n::Int) if size(m, 2)!= 4 || size(m, 1)!= 4 return m else m + Matrix(ones(4,1)) end end add_to_matrix(mymatrix, mynewrow) mymatrix.size #> (4, 4) I'm having some trouble getting the return value to the new matrix. This does not add the new row. A: I think this will do what you want: function add_to_matrix(m::Matrix, n::Int) if size(m, 2)!= 4 || size(m, 1)!= 4 return m else m += Matrix(ones(4,1)) end end Using @mzjn's answer, you could also write this

ac619d1d87