[ii jj] = find(A);
answer = unique([ii(:); jj(:)]);
should do it.
Note that the find
command with two outputs gives you the row and column index of all nonzero elements. Since you have a minimum spanning tree, each number you care about needs to occur at least once in the row or column (for example your matrix never has the number 29
in the first index, but it occurs in the second).
The unique
function makes sure that each number that occurs is only represented once.