Extracting upper triangle without diagonal continued

Hello:

I just found that there is still an issue with the following process to extract the upper triangular elements of a matrix without the diagonals if the matrix size is more than 3.

a_uv = packr(vech(diagrv(x, error(0))'));

So, I wanted to extract the upper triangular elements row wise meaning if x = { 1  2 3 4, 5 6 7 8, 9 10 11 12, 13 14 15 16}; I wanted to get 2,3,4,7,8,12 in this order, but what it does is it returns me 2,3,7,4,8,12. since vech operates row wise. Does it sound alright? Is there a quick fix for it?

Thanks

Annesha

 

 

1 Answer



0



If you know for certain that your upper triangle will not have any elements that equal 0, then you could do this:

a = {  1  2  3  4,
       5  6  7  8,
       9 10 11 12,
      13 14 15 16 }
a_uv = packr(miss(vecr(upmat(diagrv(z, 0))), 0));

If you may have elements in the upper triangle that are equal to zero, then you could do this:

a = {  1  2  3  4,
       5  6  7  8,
       9 10 11 12,
      13 14 15 16 };
a_uv = packr(vec(a) + vecr(miss(upmat(ones(rows(a), cols(a))), 1)));

or this:

a_uv = selif(vecr(a), vecr(lowmat(reshape(error(0), rows(a), cols(a)))) .== 0)

aptech

1,773

Your Answer

1 Answer

0

If you know for certain that your upper triangle will not have any elements that equal 0, then you could do this:

a = {  1  2  3  4,
       5  6  7  8,
       9 10 11 12,
      13 14 15 16 }
a_uv = packr(miss(vecr(upmat(diagrv(z, 0))), 0));

If you may have elements in the upper triangle that are equal to zero, then you could do this:

a = {  1  2  3  4,
       5  6  7  8,
       9 10 11 12,
      13 14 15 16 };
a_uv = packr(vec(a) + vecr(miss(upmat(ones(rows(a), cols(a))), 1)));

or this:

a_uv = selif(vecr(a), vecr(lowmat(reshape(error(0), rows(a), cols(a)))) .== 0)

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.