lepmueller
lepmueller

Reputation: 11

Iterator over all but one index in julia

i am trying to write a general tensor operation in julia (matrix * tensor of known rank).

My tensor is an object with n indices (e.g. tensor(a_1,a_2,...,a_n) and my matrix only has an effect on the i'th index. Therefore it would be conventient to have an iterator with goes over every but the i'th index. Is there an easy function to implement this, or do you have an idea how to do it with good performance? I also need the result of the iterator in the CartesianIndex-form (i think), because i have to iterate over the last index in a matrix-vector-multiplication style.

My first idea was to get all permutations for the indices before and after the i'th one, but the generation of those seemed tedious...

I hope you can help me, best regards, lepmueller

Upvotes: 1

Views: 348

Answers (2)

carstenbauer
carstenbauer

Reputation: 10127

FWIW, there is also a nice little package called InvertedIndices.jl which allows you to drop specific columns/rows like so:

julia> using InvertedIndices

julia> x = rand(4,4)
4×4 Array{Float64,2}:
 0.779118  0.66097   0.335433  0.583011
 0.284284  0.799394  0.353914  0.146769
 0.716189  0.605426  0.2449    0.92238
 0.140876  0.210152  0.810854  0.37236

julia> x[Not(2), Not(4)] # drop second row and 4th column
3×3 Array{Float64,2}:
 0.779118  0.66097   0.335433
 0.716189  0.605426  0.2449
 0.140876  0.210152  0.810854

Upvotes: 4

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

It's not clear exactly what you are after. You can get an iterator over the indices that ignore the i'th row of a Matrix like

Iterators.filter(x->x[1] != i, CartesianIndices(a))

Is that useful for you, or could you edit the question to be more explicit?

Upvotes: 4

Related Questions