# DimensionMismatch

**URL:** <https://discourse.julialang.org/t/dimensionmismatch/22719>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 3, 2019, 8:43pm UTC](https://discourse.julialang.org/t/dimensionmismatch/22719 "2019-04-03T20:43:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [April 3, 2019, 8:43pm UTC](https://discourse.julialang.org/t/dimensionmismatch/22719/1 "2019-04-03T20:43:07Z")

</div>

Hi

I have tried to solve matrix problems in Julia 1.1.0, not working with the following Julia code:

```julia
julia> elementDof=[1 2 3 4]
1×4 Array{Int64,2}:
 1 2 3 4

julia> stiffness=zeros(8,8)
8×8 Array{Float64,2}:
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

julia> cc = rand(4,4)
4×4 Array{Float64,2}:
 0.400909 0.031949 0.967421 0.405705
 0.662382 0.922975 0.389385 0.980334
 0.149611 0.893805 0.629037 0.74785
 0.0818064 0.533119 0.464201 0.305496

julia> stiffness[elementDof,elementDof]=stiffness[elementDof,elementDof] +cc
ERROR: DimensionMismatch("dimensions must match")

```

**This loop works in Matlab…**

```julia
elementDof=[1 2 3 4]
cc = rand(4,4)
stiffness=zeros(8,8)
stiffness(elementDof,elementDof)=stiffness(elementDof,elementDof)+cc

elementDof =

     1 2 3 4

cc =

    0.4218 0.6557 0.6787 0.6555
    0.9157 0.0357 0.7577 0.1712
    0.7922 0.8491 0.7431 0.7060
    0.9595 0.9340 0.3922 0.0318

stiffness =

     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0

stiffness =

  Columns 1 through 6

    0.4218 0.6557 0.6787 0.6555 0 0
    0.9157 0.0357 0.7577 0.1712 0 0
    0.7922 0.8491 0.7431 0.7060 0 0
    0.9595 0.9340 0.3922 0.0318 0 0
         0 0 0 0 0 0
         0 0 0 0 0 0
         0 0 0 0 0 0
         0 0 0 0 0 0

  Columns 7 through 8

         0 0
         0 0
         0 0
         0 0
         0 0
         0 0
         0 0
         0 0

```

Best

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 3, 2019, 8:46pm UTC](https://discourse.julialang.org/t/dimensionmismatch/22719/2 "2019-04-03T20:46:24Z")

</div>

> [@Aquaman](#):
>
> elementDof=[1 2 3 4]

You want

```julia
elementDof=[1, 2, 3, 4]

```

Note the commas — this is the difference between a 1-dimensional vector and a two-dimensional row of a matrix.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [April 3, 2019, 8:56pm UTC](https://discourse.julialang.org/t/dimensionmismatch/22719/3 "2019-04-03T20:56:16Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [April 3, 2019, 10:18pm UTC](https://discourse.julialang.org/t/dimensionmismatch/22719/4 "2019-04-03T22:18:30Z")

</div>

You can also use `vec()` to assure that the output is always an n-element vector.

```julia
julia> elementDof=vec([1 2 3 4])
4-element Array{Int64,1}:
 1
 2
 3
 4

```
