Page 1 of 1

subtract elements in a matrix

Posted: Thu Sep 27, 2018 10:16 am
by vytama
Hi, I have a matrix(!i,!j) and for each !i(column) I would like to create another matrix with a row lag (!j-(!j-1)). How can I achieve that? Thank you.

Re: subtract elements in a matrix

Posted: Thu Sep 27, 2018 10:50 am
by EViews Matt
Hello,

I don't yet understand what you're asking for. If the following was your initial matrix, what should the result to be?

Code: Select all

[ 1 2 3 ] [ 4 6 8 ] [ 10 15 20 ]

Re: subtract elements in a matrix

Posted: Thu Sep 27, 2018 10:55 am
by vytama
the new result should be a difference between rows:
[1-0=1 2-0=2 3-0=3]
[4-1=3 6-2=4 8-3=5]
[10-4=6 15-6=9 20-8=12]

the result after changes:

[1 2 3]
[3 4 5]
[6 9 12]

Re: subtract elements in a matrix

Posted: Thu Sep 27, 2018 12:04 pm
by EViews Matt
I understand now, after the first row you want the row differences. For an initial matrix m, you can use something like the following:

Code: Select all

matrix result = m - @vcat(@filledvector(m.@cols, 0), @subextract(m, 1, 1, m.@rows - 1))

Re: subtract elements in a matrix

Posted: Thu Sep 27, 2018 12:06 pm
by vytama
thank you.