如何在朱莉娅的一个for循环中实现5维数组?

时间:2015-03-06 17:12:49

标签: arrays loops julia dynamic-arrays

function prealloc()
    situation=zeros(Int64,3^5,5);
    i=1;
    for north=0:2
        for south=0:2
            for east=0:2
                for west=0:2
                    for current=0:2
                        situation[i,:]=[north, south, east, west, current]
                        i+=1
                    end
                end
            end
        end
    end
    situation
end
prealloc()

如何在一个for循环上实现此代码 或者只在一个循环中 我如何使用嵌套循环来做到这一点

1 个答案:

答案 0 :(得分:4)

您可以将for循环组合成一个包含多个变量的循环。

function prealloc()
    situation=zeros(Int64,3^5,5);
    i=1;
    for north=0:2, south=0:2, east=0:2, west=0:2, current=0:2
        situation[i,:]=[north, south, east, west, current]
        i+=1
    end
    situation
end