Transposed matrix


I’m new to Julia and I apologize in advance if there’s a simple answer to my question somewhere but I’ve spent lots of time without any success.

I’m trying to re-write the following Python code in Julia

import scipy as sp
psi = sp.ones((1000, 1))
idx_blk = range(15)
x = sp.diag(1.0/psi[idx_blk].T[0])

My problem is I don’t know exactly what the [0] in the code above is doing (more of a Python issue but was hoping someone here might have a solution).


using LinearAlgebra
psi = ones(1000, 1)
idx_blk = 1:15
x = Diagonal(1.0 ./ transpose(psi[idx_blk]))


1 Like
>>> psi[idx_blk].T
array([[1., 1., 1., 1., 1.]])

Notice the double brackets [[, compared to

>>> psi[idx_blk].T[0]
array([1., 1., 1., 1., 1.])

so the [0] is pulling out the first array from an “array-of-arrays” (a type of matrix?).

To get the same result in Julia you can do this:

using LinearAlgebra
psi = ones(10, 1)
idx_blk = 1:5
x = diagm(1.0./psi[idx_blk])

Note I’ve reduced the numbers. You don’t need to use transpose either.