Page 1 of 1

reshaping a matrix

Posted: Fri Jan 14, 2011 6:16 am
by facosta
Hello there. Thank you guys for all the help in the forum.
I am programming in Eviews and I would like to know if there is any way to reshape a matrix by a single command. For example, in GAUSS programming, you use: "reshape(name_of_ the_ old_matrix, new_n°_rows, new_n°_cols)".
Can you use a smmilar code to do this in Eviews 7?
Thank you!
Best regards.

Re: reshaping a matrix

Posted: Fri Jan 14, 2011 9:03 am
by EViews Gareth
Unfortunately not. Possibly the easiest thing to do is to vec it, and then unvec it.

Re: reshaping a matrix

Posted: Fri Jan 14, 2011 1:05 pm
by EViews Glenn
Gareth is correct that we don't have a built-in, and he is right about @unvec, but I think he undersells the existing tools somewhat since we can feed the output from one matrix function into another. Thus, the EViews command

Code: Select all

matrix newmat = @transpose(@unvec(@vec(@transpose(oldmat)), new_n_cols))
should be the equivalent of the Gauss reshape command. The @vec converts the original matrix into a vector and the @unvec converts back into a matrix. I've verified this with a couple of examples from the Gauss manual, but you should double-check with a few others. The reason it is a bit more complicated than it otherwise might be is that Gauss stores matrices in row-major order so we have to work in transpose space.

I should note that unlike Gauss, the code above will not repeat observations to fill in larger matrices. For that, you should use some variant of the .fill command to get the original matrix to have the right total number of elements.

Re: reshaping a matrix

Posted: Mon Jan 17, 2011 6:07 am
by facosta
Thank you so much.
It worked perfectly.
Regards.