This difference should be explained in the documentation for diag The current documentation is kind of short:
Base.diag(M[, k]) The "k"-th diagonal of a matrix, as a vector. Ivar kl. 21:54:43 UTC+2 søndag 27. april 2014 skrev John Code følgende: > > Thank you. > > On Sunday, April 27, 2014 11:49:12 PM UTC+4, Andreas Noack Jensen wrote: >> >> Hi John >> >> In julia, the function diag extract the diagonal of a matrix and if the >> matrix is rectangular, it extracts the diagonal of the largest square sub >> matrix. Note that in julia, [1 2 3 4] is not vector but a matrix. To >> construct a matrix from a vector you can either use the function diagm, >> which does what you expected diag did, >> >> julia> diagm([1,2,3,4]) >> 4x4 Array{Int64,2}: >> 1 0 0 0 >> 0 2 0 0 >> 0 0 3 0 >> 0 0 0 4 >> >> but it is often better to use Diagonal, which creates a special Diagonal >> matrix, >> >> julia> Diagonal([1,2,3,4]) >> >> 4x4 Diagonal{Int64}: >> 1 0 0 0 >> 0 2 0 0 >> 0 0 3 0 >> 0 0 0 4 >> >> >> 2014-04-27 21:40 GMT+02:00 John Code <jcod...@gmail.com>: >> > >> > Hi all, >> > I would like to ask why there is a difference between Octave diag >> function >> > and the function that julia provide. For example, in the following >> Octave session I get: >> > >> > ============================ >> > octave:1> v = [1 2 3 4] >> > v = >> > >> > 1 2 3 4 >> > >> > octave:2> a = diag(v) >> > a = >> > >> > Diagonal Matrix >> > >> > 1 0 0 0 >> > 0 2 0 0 >> > 0 0 3 0 >> > 0 0 0 4 >> > ============================= >> > >> > But in Julia I get: >> > >> > ============================= >> > julia> v = [1 2 3 4] >> > 1x4 Array{Int64,2}: >> > 1 2 3 4 >> > >> > julia> a = diag(v) >> > 1-element Array{Int64,1}: >> > 1 >> > >> > >> > ============================= >> > >> > >> > Why is this the case and how to get a similar effect of the octave code. >> > Thank you. >> >> >> >> >> -- >> Med venlig hilsen >> >> Andreas Noack Jensen >> >