Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Iterating over and slicing a multidimensional array

$
0
0

Is it possible to iterate over a multidimensional array and slice the different dimensions in a loop? For example instead of

julia> a = zeros(2,2)
2×2 Array{Float64,2}:
 0.0  0.0
 0.0  0.0

julia> for i in 1:2
           a[i,:] = ones(2)
       end

julia> for j in 1:2
           a[:,j] = j * ones(2)
       end

Is there a possibility to write something like

julia> for dim in 1:2
            for i in 1:size(a)[dim]
                a[slice(i,dim)] = i * ones(2)
            end
        end

wher slice(i,1) would create Indices similar to i,:.

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles