phdstudent
phdstudent

Reputation: 1144

Non-trivial reshape in chunks

I am trying to reshape a matrix, but not in the standard way. It is basically a "chunk" reshape. I have a column vector named matrix1 which is (T*N x 1) and a matrix named matrix2 which is TxN. I want the first N elements of the column vector matrix1 to be transposed into the first row of matrix2. Then the second chunk of N elements of vector matrix1 to be transposed into the second row of matrix2.

I know how to do it with a loop (see code below). Just wondering if there is a smarter way to do it.

T = 2;
N = 7;

matrix1 = rand(T*N,1);
matrix2 = NaN(T,N);

for t = 1:T
    matrix2(t,:) = matrix1(t*N-N+1:t*N,1)';
end

Upvotes: 0

Views: 44

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

Use reshape for reshaping... You literally describe a standard reshape in the text.

reshape(matrix1,N,T).'

Upvotes: 2

Related Questions