Hi I have a rather simple question. Can anybody please tell me how can I extract the upper triangle portion of a matrix without its diagonal elements in a vector?
if a = { 1 2 3, 4 5 6, 7 8 9} is a matrix I want to extract 2,3,6 in this order, is there a quick way to do that without using loop?
Thanks
Annesha
2 Answers
0
I am not sure this is the absolute best method, but it is certainly better than a for loop:
One line solution
a_uv = packr(vech(diagrv(x, error(0))'));
Verbose 'proc' with explanation of steps
a = { 1 2 3, 4 5 6, 7 8 9 }; a_uv = upmatND(a); proc (1) = upmatND(x); //replace diagonal with missing values //error(0) is equivalent to a missing x = diagrv(x, error(0)); //Remove the lower triangle of //transposed matrix and return //as a vector x = vech(x'); //Remove diagonal elements //which we set to missing values //above x = packr(x); retp(x); endp;
0
Thank you much, this is exactly what I was looking for.
Your Answer
2 Answers
I am not sure this is the absolute best method, but it is certainly better than a for loop:
One line solution
a_uv = packr(vech(diagrv(x, error(0))'));
Verbose 'proc' with explanation of steps
a = { 1 2 3, 4 5 6, 7 8 9 }; a_uv = upmatND(a); proc (1) = upmatND(x); //replace diagonal with missing values //error(0) is equivalent to a missing x = diagrv(x, error(0)); //Remove the lower triangle of //transposed matrix and return //as a vector x = vech(x'); //Remove diagonal elements //which we set to missing values //above x = packr(x); retp(x); endp;
Thank you much, this is exactly what I was looking for.