필터 지우기
필터 지우기

Question about concatenation in for-loop

조회 수: 6 (최근 30일)
Patrick Keiffer
Patrick Keiffer 2019년 1월 9일
댓글: Patrick Keiffer 2019년 1월 9일
Hello,
I am trying to concatenate a 2D matrix into a 3D matrix where the length of the 3rd dimension is dependent on length of a vector previously asigned. For example, given
A = [1,2,3,4,5];
B = [1,2;3,4];
I would like creat marix C with size 2x2x5. I can do this easily by doing
C = cat(3,B,B,B,B,B);
However if I change the length of A, I want to write my code so that I don't have to update then number of times "B" repeats itself inside the cat function. I tried using the folowing for-loop but it didn't work. Any advice would be appreciated.
for i=1:length(A)
C = cat(3,B,B)
end
  댓글 수: 1
Patrick Keiffer
Patrick Keiffer 2019년 1월 9일
So I figured it out. The folowing code works.
A = [1,2,3,4,5];
B = [1,2;3,4];
C = B;
for i=1:length(A)
C = cat(3,C,B);
end

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

채택된 답변

OCDER
OCDER 2019년 1월 9일
Instead of cat, use repmat for your case, as you're repeatedly stacking a matrix in a dimension.
B = rand(2, 5)
C = repmat(B, 1, 1, 5) %stacks B in the 3rd dimension 5 times

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by