julia find in matrix with (row,col) instead of index -
julia find in matrix with (row,col) instead of index -
in julia can find coordinates of elements in matrix via:
julia> find( x -> x == 2, [ 1 2 3; 2 3 4; 1 0 2] ) 3-element array{int64,1}: 2 4 9
these values right prefer (row,col) tuples instead.
(1,2) (2,1) (3,3)
what easiest way accomplish in julia?
i don't believe there inbuilt way it, here function it
function findmat(f, a::abstractmatrix) m,n = size(a) out = (int,int)[] in 1:m, j in 1:n f(a[i,j]) && push!(out,(i,j)) end out end
e.g.
julia> findmat(x->x==2, [ 1 2 3; 2 3 4; 1 0 2] ) 3-element array{(int64,int64),1}: (1,2) (2,1) (3,3)
if big number of items satisfy status might more efficient in 2 passes, uncertainty it.
matrix find julia-lang
Comments
Post a Comment