Extend a matrix by extending its vectors

조회 수: 2 (최근 30일)
f10w
f10w 2014년 5월 29일
댓글: f10w 2014년 5월 30일
I have an d*m*n 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 of dimension d: (1,2,...,d) (it's (x_1,x_2,...,x_n) 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)
There are several ways to extend a single vector, as already suggested here. Suppose that a function called 'extend' does that job. Then, to extend every vector in the matrix A, we can do:
B = zeros(d*d,m,n);
for i = 1:n
for j = 1:m
B(:,j,i) = extend(A(:,j,i);
end
end
However, this is quite slow because of the loop. Could anybody please suggest me a fast way to do that?
Thank you in advance for your help.
  댓글 수: 2
José-Luis
José-Luis 2014년 5월 29일
What makes you thing it's slow because of the loop? Not all loops are evil. It would depend on what your extend() function looks like.
f10w
f10w 2014년 5월 30일
Hmm, because using a loop is slow in general (but I may be wrong :P).

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 5월 29일
B = A(kron((1:d)',ones(d,1)),:,:);
  댓글 수: 1
f10w
f10w 2014년 5월 30일
Thanks, Andrei. That answers my question.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by