Search code examples
matlabsparse-matrixminimum-spanning-tree

Sparse and Tril functions exceptions


Can anyone explain me why the code below doesnt work and returns me

W = [.34 .34 ];
DG = sparse([1 2],[2 3],W);

UG = tril(DG + DG')

??? Error using ==> plus Matrix dimensions must agree.

and the code below works properly ?

W = [.34 .34 .34];
DG = sparse([1 2 3],[2 3 1],W);
UG = tril(DG + DG')

Solution

  • In the first example, the size of DG is 2 by 3 and therefore adding DG and DG' will cause an matrix dimension error. In the second example the matrix DG is 3 by 3, hence no error when adding DG + DG'.