Julia Array连接维度不匹配

时间:2014-11-05 23:38:56

标签: arrays multidimensional-array julia

使用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)

如果我尝试将它们组合起来,我会收到此错误:

mismatch in dimension 2

变量x和a在控制台中的尺寸相同:

println(x)

[0.7307313376278893 0.9102437792092966 1.0897562207907034 1.2692686623721108]

println(a)

[0.0,0.0,0.0]

但是x是第二个维度。有没有办法组合数组,所以我可以进入维度1?

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

1 个答案:

答案 0 :(得分:2)

问题在于,通过转置x(在行尾添加'),您最终会得到以下结果:

julia> size(x)
(1,4)

julia> size(a)
(3,)

所以,当你尝试y=[x,a]朱莉娅正当地抱怨它无法连接它们时。

有(至少)两种解决方案:

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)也转置a并连接而不用逗号:

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

a = zeros(3)'

println(x)

y =[x a]

print (y)

在第一种情况下,您将拥有size(y) = (7, 1),在第二种情况下,您将拥有size(y) = (1,7),因此您选择的选项取决于您希望的y大小。