Matrix reshape in row-wise and column-wise

363 Views Asked by At

Assume I have a matrix X whose dimension is (16,16). That matrix is reshaped into X1 = (256,1) but in row-wise, it means starting the first row then second row and so on. On the other hand, if matrix X was reshaped into X2 = (256,1) too, but column-wise, it means starting first columns, then second column and so on.

My question, if we can write X1 = PX2, where P is a permutation matrix of dimension (256,256), What should be the matrix P?

Thank you

1

There are 1 best solutions below

5
On BEST ANSWER

Let's start all indexing from $0$.

You have $X_1 = (x_{0,0}, x_{0,1}, \cdots, x_{15,14},x_{15,15})$ and $X_2 = (x_{0,0}, x_{1,0}, \cdots, x_{14,15},x_{15,15})$.

Hence the elements must equal: $X_1(k=16\cdot i + j)=X_2(n=16\cdot j + i)$. This gives you a direct relation for the matrix $P$ with element $p_{k,n}$ which maps $k$ to $n$.

From the first equation, you have $k=16\cdot i + j$ which gives $j = k \mod 16$ and $i = {\rm{quotient}}(k,16)$, hence $n$ can be computed immediately. So, for all $k$,

$$ p_{k,n} = 1 \; {\rm{for }}\; n = 16\cdot (k \mod 16) + {\rm{quotient}}(k,16)\\ p_{k,n} = 0 \; {\rm{for}}\; {\rm{all }}\; {\rm{other }} \; n $$

Example 1: (obvious) $p_{0,0} = 1$

Example 2: $k=1$ gives $n = 16\cdot (1 \mod 16) + {\rm{quotient}}(1,16) = 16\cdot 1 + 0 = 16$, hence $p_{1,16} = 1$. This corresponds to the values $i=0$, $j=1$.

Example 3: $k=18$, gives $n = 16\cdot (18 \mod 16) + {\rm{quotient}}(18,16) = 16\cdot 2 + 1 = 33$, hence $p_{18,33} = 1$. This corresponds to the values $i=1$, $j=2$.


Here is a little matlab program which can be copied verbatim which does it. Call p(2) to get an idea. Your case is p(16).


function p(v)

%v = vectorsize, in your case 16

for k=1:v^2

for n = 1:v^2

    p(k,n)=0;

end;

end;

for i=1:v

for j = 1:v

    k=v* (i-1) + j;

    n=v* (j-1) + i;

    p(k,n)=1;

end;

end;

disp(p)