简体   繁体   中英

Julia Array Concatenation dimension mismatch

I have a dimensional mismatch problem when using y =[x,a] to concatenate my two arrays:

x = reshape(1:16, 4, 4)
x = mean((x ./ mean(x,1)),2)'

a = zeros(3)

println(x)

y =[x,a]

print (y)

If I try combining them I will get this error:

mismatch in dimension 2

Both variables x and a appear to be in the same dimensions in the console:

println(x)

[0.7307313376278893 0.9102437792092966 1.0897562207907034 1.2692686623721108]

println(a)

[0.0,0.0,0.0]

But x is in the second dimension. Is there a way to combine the arrays so I can get in dimension 1?

y = [0.7307313376278893 0.9102437792092966 1.0897562207907034 1.2692686623721108, 0.0,0.0,0.0]

The problem is that by transposing x (putting a ' at the end of the line) you end up with the following:

julia> size(x)
(1,4)

julia> size(a)
(3,)

So when you try y=[x,a] Julia rightfully complains that it cannot concatenate them.

There are (at least) two solutions:

1) Don't transpose x:

x = reshape(1:16, 4, 4)
x = mean((x ./ mean(x,1)),2)

a = zeros(3)

println(x)

y =[x,a]

print (y)

2) also transpose a and concatenate without a comma:

x = reshape(1:16, 4, 4)
x = mean((x ./ mean(x,1)),2)'

a = zeros(3)'

println(x)

y =[x a]

print (y)

In the first case you will have size(y) = (7, 1) and in the second case you will have size(y) = (1,7) , so which option you choose will depend on what you want for the size of y .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM