f10w
f10w

Reputation: 1586

Matlab: Extend a matrix by extending its vectors

I have a dxmxn matrix A (interpretation: for each n, there are m vectors of dimension d). I would like to extend each d-dimensional vector as follows:

Consider a vector v of dimension d: (1,2,...,d) (it's (x_1,x_2,...,x_d) but I removed the 'x_' for simplicity). The goal is to extend v to obtain a d*d vector of the form:

(1,1,...,1,2,...,2,...,d,d,...,d)

Could anybody please suggest me a fast way to do that? (I guess that using a loop and extending each vector at each iteration is very slow.)

Thank you in advance for your help.

Upvotes: 3

Views: 168

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

reshape(A(ceil(1/d:1/d:numel(A))), d^2,m,n)

Upvotes: 0

Divakar
Divakar

Reputation: 221514

Damn!

reshape(permute(repmat(permute(A,[1 4 2 3]),[1 d]),[ 2 1 3 4]),d*d,m,n)

Upvotes: 3

Related Questions