DimensionMismatch

question
#1

Hi

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

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…

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

0 Likes

#2

You want

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.

1 Like

#3

Thank you!

0 Likes

#4

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

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