Adding a line to the end of an Array

I have a 18×12 Array{Any,2} called “stock”. I also have a 12×1 vector called “flow”.

My aim is to add the content of flow to the end of stock (essentially adding a new row to stock).

I have tried

 stock = [stock;flow']

But I get the error “MethodError: no method mathing adjoint(::String)”

How can I get the job done?

julia> a=[1 2 3;4 5 6]
2×3 Array{Int64,2}:
 1  2  3
 4  5  6

julia> b=[7 8 9 ]
1×3 Array{Int64,2}:
 7  8  9

julia> vcat(a,b)
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

You may have to transpose your flow:
vcat(stock,flow')

I think the basic problem is that flow’ does not work:

image

Try permutedims(flow).

3 Likes

Is this basically the same problem you posted an hour ago?

1 Like

My other question did not contain the necessary information, so I requested it deleted and then posted this update. Sorry for any inconvenience.

You can just update the thread instead. Now there are two threads about adding stuff to a matrix in the most recent thread list.

1 Like

Just to summarize the answer to your question from the different posts:

You are trying to add a 12 element vector as a row to an 18x12 matrix. You can think of this as vertically concatenating the two. Vectors in Julia are however column vectors by default, and you are looking to append a row vector, so you need to permute the dimensions:

julia> a = rand(3, 2)
3×2 Array{Float64,2}:
 0.387527  0.230343
 0.682296  0.255378
 0.22481   0.838333

julia> b = rand(2)
2-element Array{Float64,1}:
 0.24946180391946426
 0.24528450122180612

julia> vcat(a, b) # doesn't work as b is 2x1 rather than 1x2
ERROR: ArgumentError: number of columns of each array must match (got (2, 1))
Stacktrace:
 [1] _typed_vcat(::Type{Float64}, ::Tuple{Array{Float64,2},Array{Float64,1}}) at .\abstractarray.jl:1358
 [2] typed_vcat at .\abstractarray.jl:1372 [inlined]
 [3] vcat(::Array{Float64,2}, ::Array{Float64,1}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\SparseArrays\src\sparsevector.jl:1078
 [4] top-level scope at REPL[114]:1

julia> vcat(a, b')
4×2 Array{Float64,2}:
 0.387527  0.230343
 0.682296  0.255378
 0.22481   0.838333
 0.249462  0.245285

vcat is the same as doing [a; b'], i.e. the array syntax with a semicolon is syntactic sugar for vcat:

julia> [a; b']
4×2 Array{Float64,2}:
 0.387527  0.230343
 0.682296  0.255378
 0.22481   0.838333
 0.249462  0.245285

Your other problemn is that your b includes non-numerical values, for which ', which produces an adjoint, doesn’t work - see the discussion here. You therefore need permutedims to get your vector into a row vector.