Using repelem without prior knowledge of the number of matrix dimensions

조회 수: 5 (최근 30일)
Hello,
I have a matrix A that can be either 3D, 4D, 5D etc. I want to repeat the elements of the last dimension x times. This can be accomplished by using the following expressions: repelem(A,1,1,x) for 3D and repelem(A,1,1,1,x) for 4D matrices. However, since I do not know the number of matrix dimensions in advance, these expressions do not help me. Ideally, I would like to use repelem like repmat e.g. repelem(A,[1,1,x]) for 3D and repelem(A,[1,1,1,x]) for 4D matrices. However these expressions are not valid. I know I can do this combining reshape and repmat but I would like to know if there is a more compact way since there is a repelem function (I would also like to avoid using eval). Any ideas?

채택된 답변

Stephen23
Stephen23 2017년 2월 6일
편집: Stephen23 2017년 2월 6일
It is easy to generate a comma separated list from a cell array, and use this to specify the repelem inputs:
x = 10;
C = num2cell([ones(1,ndims(A)-1),x]);
out = repelem(A, C{:})
Note that you will need to consider scalar and column vector A as special cases.
  댓글 수: 1
Emmanouil
Emmanouil 2017년 2월 6일
Works like a charm! I did not know I could do this with a cell array. Thanx!

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

추가 답변 (1개)

KSSV
KSSV 2017년 2월 6일
편집: KSSV 2017년 2월 6일
Use a if condition on length(size(A)). If A is 3D your length(size(A)) will be 3 and you have to use repelem(A,1,1,x). Like wise if A is 4D, length(size(A)) will be 4.
  댓글 수: 3
KSSV
KSSV 2017년 2월 6일
Let it be..you can generate [1 1 x] using [ones(length(size(A))-1) x]
Emmanouil
Emmanouil 2017년 2월 6일
I know I can do this, this was my initial intention as I write in the question. The problem is that the expression repelem(A,[1,1,x]) is not valid.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by