arrays - 如何在 julia 的一个 for 循环中实现 5 维数组?

标签 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 循环上实现此代码 或仅在一个循环中 我如何使用嵌套循环来做到这一点

最佳答案

您可以将 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

关于arrays - 如何在 julia 的一个 for 循环中实现 5 维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28904008/

相关文章:

java - 二十一点游戏的逻辑

julia - Julia 中是否有用于函数组合的运算符?

julia - 将参数限制为 Julia 函数签名中的一组值

r - 如何在 R 中创建迭代 lm() 公式

php - 关联数组的最大值

java - 仅从自定义数组列表中获取字符串并使用字符串创建字符串数组

c++ - 如何在 C++ 中将动态数组转换为静态数组?

javascript - HTML5 Canvas - 暂停时耗时

Julia 相当于 python 中的 "not"

php - 在订单数组中查找重复值并替换