简体   繁体   English

Julia数组串联维度不匹配

[英]Julia Array Concatenation dimension mismatch

I have a dimensional mismatch problem when using y =[x,a] to concatenate my two arrays: 使用y =[x,a]连接两个数组时,我遇到了尺寸不匹配的问题:

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: 变量x和a在控制台中似乎具有相同的尺寸:

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. 但是x在第二维。 Is there a way to combine the arrays so I can get in dimension 1? 有没有一种方法可以将数组组合在一起,以便获得维度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: 问题是,通过转置x(在行的末尾添加' ),您将得到以下结果:

julia> size(x)
(1,4)

julia> size(a)
(3,)

So when you try y=[x,a] Julia rightfully complains that it cannot concatenate them. 因此,当您尝试y=[x,a] Julia正确地抱怨它无法将它们连接起来。

There are (at least) two solutions: 有(至少)两种解决方案:

1) Don't transpose x: 1)不要移置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: 2)也转置a并串联而不用逗号:

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 . 在第一种情况下,你将有size(y) = (7, 1)和在第二种情况下,你将有size(y) = (1,7)所以哪个选项您选择将取决于你想要的大小y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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