Taking a matrix and creating a 2Tuple with first element Row number, and second element row?

Hello guys, I hope you are doing fine.

I would like to take a matrix and create a Tuple, where the first element is the row number (an index) and the second element is the row (a vector).

graph =[0 2 4 0 0 0; 0 0 1 7 0 0; 0 0 0 0 3 0;
        0 0 0 0 0 1; 0 0 0 2 0 5; 0 0 0 0 0 0];

I would like to get a tuple in the following way:

6-element Tuple
(1,[0 2 4 0 0 0])
(2,[0 0 1 7 0 0])
(3,[0 0 0 0 3 0])
(4,[0 0 0 0 0 1])
(5,[0 0 0 2 0 5])
(6,[0 0 0 0 0 0])

With a vector it is easy because

f=collect(enumerate(graph[1,:]))

returns an f=
(1,0)
(2,2)
(3,4)
(4,0)
(5,0)
(6,0)

How can I create a Tuple with the rows of the Matrix as second element? Any thoughts?

Thanks in advance.

This solves your problem?

julia> result = collect(enumerate(eachrow(graph)))
6-element Vector{Tuple{Int64, SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}}:
 (1, [0, 2, 4, 0, 0, 0])
 (2, [0, 0, 1, 7, 0, 0])
 (3, [0, 0, 0, 0, 3, 0])
 (4, [0, 0, 0, 0, 0, 1])
 (5, [0, 0, 0, 2, 0, 5])
 (6, [0, 0, 0, 0, 0, 0])

Thank you so much! It works perfectly!

1 Like