Skip to main content
added 6 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I often want to iterate either row-wise or column-wise though a matrix in julia. So iJulia, 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 Useuse:

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 forloopsfor loops).
  • I feel like there should be a inbuild function for this, but the closest iI know of is mapslice.

I often want to iterate either row-wise or column-wise though a matrix in julia. So i 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 forloops)
  • I feel like there should be a inbuild function for this, but the closest i know of is mapslice

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.
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link

Functions for outputting the rows and columns of a matrix

I often want to iterate either row-wise or column-wise though a matrix in julia. So i 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 forloops)
  • I feel like there should be a inbuild function for this, but the closest i know of is mapslice