Scilab syntax: How to transpose and reshape without the use of an intermediate variable?

jofafrazze picture jofafrazze · Nov 28, 2012 · Viewed 14.5k times · Source

Lets use the matrix A as an example:

-->A = [1 2 3; 4 5 6]
 A  =

    1.    2.    3.  
    4.    5.    6.  

I can transpose this matrix:

-->A'
 ans  =

    1.    4.  
    2.    5.  
    3.    6.  

...and I can reshape this matrix into a single column:

-->A(:)
 ans  =

    1.  
    4.  
    2.  
    5.  
    3.  
    6.  

...but I cannot transpose and reshape in a single line or without using a intermediate variable:

-->A'(:)
     !--error 276 
Missing operator, comma, or semicolon.

-->B = A'; B(:)
 ans  =

    1.  
    2.  
    3.  
    4.  
    5.  
    6.  

Is there a way to accomplish this without the intermediate variable?

Answer

xenoclast picture xenoclast · Dec 5, 2012

Although the transpose operator doesn't seem to have a keyword equivalent the (:) syntax does: matrix.

So the equivalent of A(:) would be matrix(A,1,-1) such that you're reshaping to 1 column and 'however many' rows (the -1 argument). Thus if you feed A' into that you get the row vector in the desired order

-->matrix(A',1,-1)
 ans  =

    1.    2.    3.    4.    5.    6.

This works with the conjugate transpose operator too (A.').