簡體   English   中英

如何在Julia中在for循環中實現5維數組?

[英]How can I implement 5 dimensional array in one for loop in julia?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM