I often want to iterate either row-wise or column-wise though a matrix in Julia, so I've created a pair of functions to help:
function cols(x::Matrix)
function _it()
for ii in 1:size(x,2)
produce(x[:,ii])
end
end
Task(_it)
end
function rows(x::Matrix)
function _it()
for ii in 1:size(x,1)
produce(x[ii,:])
end
end
Task(_it)
end
Example of use:
AA = [1 2 3; 1 2 3]
println("Columns are:")
for ii in cols(AA)
println(ii)
end
println("\nRows are:")
for ii in rows(AA)
println(ii)
end
Outputs:
Columns are:
[1,1]
[2,2]
[3,3]
Rows are:
[1 2 3]
[1 2 3]
Possible issues:
- It seems like this could be one function, that just takes the dimension as a parameter.
- This might confound the JIT, which would mean losing out out a bunch of speed. (The Julia JIT can normally vectorize
for
loops). - I feel like there should be a inbuild function for this, but the closest I know of is mapslice.