Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a vector to Julia matrix as a row?

I have an empty matrix initially:

m = Matrix(0, 3)

and a row that I want to add:

v = [2,3]

I try to do this:

[m v]

But I get an error

 ERROR: ArgumentError: number of rows of each array must match

What's the proper way to do this?

like image 763
becko Avatar asked Feb 01 '16 09:02

becko


People also ask

What is the difference between Julia vectors and matrix?

For Julia, Vectors are just a special kind of Matrix, namely with just one row (row matrix) or just one column (column matrix): Julia Vectors can come in two forms: Column Matrices (one column, N rows) and Row Matrices (one row, N columns)

How to create vectors in Julia?

Technically, there's still another way to create Vectors in Julia but they are not as widely used and will probably cause some confusion. Remember that you can create an Array of Strings (or any other type, really) using commas between the elements: So you can also create an Array of Integers: These are commonly referred to as 1-D Arrays.

How do you concatenate matrices in Julia?

In Julia we can concatenate a matrix to another matrix to the right side of the initial matrix or to the bottom of it. We use vcat (A, B) to concatenate to the side. And hcat (A, B) to concatenate to the bottom.

How to get the size of a matrix in Julia?

We can also get the size of a matrix using size (A). The transpose operation flips the matrix over its diagonal by switching the rows and columns. Let A be a matrix. We can get the transpose of A by using A’. A matrix in Julia can be flipped via the X-axis i.e. horizontally or via the Y-axis i.e. vertically.


Video Answer


2 Answers

That is because your matrix sizes don't match. Specifically v does not contain enough columns to match m. And its transposed

So this doesnt work

m = Matrix(0, 3)
v = [2,3]
m = cat(1, m, v)  # or a = [m; v]
>> ERROR: DimensionMismatch("mismatch in dimension 2 (expected 3 got 1)")

whereas this does

m = Matrix(0, 3)
v = [2 3 4]
m = cat(1, m, v)  # or m = [m; v]
>> 1x3 Array{Any,2}:
>>   2  3  4

and if you run it again it creates another row

m = cat(1, m, v)  # or m = [m; v]
>> 2x3 Array{Any,2}:
>>   2  3  4
>>   2  3  4
like image 51
Rob Avatar answered Sep 23 '22 21:09

Rob


Use the vcat (concatenate vertically) function:

help?> vcat
search: vcat hvcat VecOrMat DenseVecOrMat StridedVecOrMat AbstractVecOrMat levicivita is_valid_char @vectorize_2arg

  vcat(A...)

  Concatenate along dimension 1

Notice you have to transpose the vector v, ie. v', else you get a DimensionMismatch error:

julia> v = zeros(3)
3-element Array{Float64,1}:
 0.0
 0.0
 0.0

julia> m = ones(3, 3)
3x3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> vcat(m, v')    # '
4x3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
 0.0  0.0  0.0

julia> v'    # '
1x3 Array{Float64,2}:
 0.0  0.0  0.0

julia> vcat(m, v)
ERROR: DimensionMismatch("mismatch in dimension 2 (expected 3 got 1)")
 in cat_t at abstractarray.jl:850
 in vcat at abstractarray.jl:887

Note: the comments; # ' are there just to make syntax highlighting work well.

like image 36
HarmonicaMuse Avatar answered Sep 20 '22 21:09

HarmonicaMuse