Soma
Soma

Reputation: 743

How is it possible to change a matrix in Julia?

Would you please help me? I have this matrix

> rout
4×5 Array{Int64,2}:
 0  1  4  0  2
 0  3  0  1  2
 0  2  4  0  3
 0  1  4  2  3

each row of this matrix has some sections. for example first row has two sections. section one includes 1,4 and section two includes 2 and last row has one section 1,2,3,4. I want to get a matrix which the number of sections in each row seat as value of another matrix (nrout[i,j]). for before rout matrix, this nrout can be made:(columns of nrout are 1,2,3,4)

> nrout
4×4 Array{Int64,2}:
 1  2  0  1
 2  2  1  0
 0  1  2  1
 1  1  1  1

instead 1 in rout seats 1 (the number of section 1) in nrout. instead 4 puts 1(the number of section 1) in nrout. instead 2 puts 2 (the number of section 2) in nrout.

would you pleas help me that how this matrix can be made in Julia?

Upvotes: 2

Views: 1304

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69869

I would use looping for this problem:

function getnrout(rout)
    # assume we have at least one 0 in each row in column 1
    nrout = zeros(Int, size(rout, 1), size(rout, 2) - 1)
    for i in axes(rout, 1)
        section = 0
        for j in axes(rout, 2)
            v = rout[i,j]
            if v == 0
                section += 1
            else
                nrout[i, v] = section
            end
        end
    end
    return nrout
end

Upvotes: 1

Related Questions