From: AlexHuber Huber on
Hi,
I have got Matrix A and I want to save the upper triangular part of the Matrix A in a vector b.

For example A=[1,2,3;4,5,6;7,8,9]
then then function should do the following:
function(A)=[1;2;3;5;6;9]

I know how to do it with a for loop, but I feel there are far easier solutions :)

(I need this to fit a VAR on a matrix)

Thanks,
Michael
From: mikin Nede on
"AlexHuber Huber" <mischamail(a)web.de> wrote in message <hpka9e$561$1(a)fred.mathworks.com>...
Hi Alex, just do it:

nonzeros(triu(A)')

Cheers,
Mik
From: AlexHuber Huber on
"mikin Nede" <mnedelj(a)gmail.com> wrote in message <hpkbet$l4m$1(a)fred.mathworks.com>...
> "AlexHuber Huber" <mischamail(a)web.de> wrote in message <hpka9e$561$1(a)fred.mathworks.com>...
> Hi Alex, just do it:
>
> nonzeros(triu(A)')
>
> Cheers,
> Mik

Wow that works, thanks very nice!
From: Bruno Luong on
>
> nonzeros(triu(A)')
>

This won't work if the upper triangular part has zero(s).

Use this tool:
http://www.mathworks.com/matlabcentral/fileexchange/23391-triangular-and-diagonal-indexing

Bruno
From: vortse a on
You can use triu on a logical matrix and avoid using nonzeros:

>>B=A(triu(true(size(A))))

However this aggregates B columnwise. If this is unwanted, then there is some transposing to be done, but I was unable to conjure it.