Extracting upper triangle without diaginal

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?






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
    x = packr(x);

Thank you much, this is exactly what I was looking for.

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.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy