Prevent Julia from automatically converting type 1D matrix fragment

alpha = [1 2 3; 4 5 6] alpha[:, 1] # Type is Array{Int64, 1} alpha[:, 1:2] # Type is Array{In64, 2} 

I just want to prevent automatic type conversion, but it's very difficult for me to figure out how to do this. Yes, I could just go alpha[:, 1]'' , but I want to prevent memory redistribution. There is vec() to go in the other direction (1xn matrix), but I cannot find the save function of the matrix (nx1) matrix.

+6
source share
1 answer

Use length range 1 instead of index

Instead of simply specifying the index ( Int64 ) of the desired column, specify a range ( UnitRange{Int64} ) of length 1: 1:1 .

This will cause Julia to save the type of the 2D array ( Array{Int64,2} ) instead of returning the vector ( Array{Int64,1} ).

Edit : the developers discussed this section here (thanks to Colin for pointing me to it ).

 julia> alpha = [1 2 3; 4 5 6] 2x3 Array{Int64,2}: 1 2 3 4 5 6 julia> alpha[:,1] # nope 2-element Array{Int64,1}: 1 4 julia> alpha[:,1:1] # yep 2x1 Array{Int64,2}: 1 4 
+6
source

Source: https://habr.com/ru/post/978741/


All Articles