필터 지우기
필터 지우기

building a 3D matrix

조회 수: 1 (최근 30일)
Qian
Qian 2012년 10월 30일
댓글: Gowthami Odeti 2017년 11월 9일
I understand how the following works, x=[a b; c d]; x(:,:,2)=x; which will give x as 3-D matrix. However, i'm writing a code for which i need x to be updated each time. for example, for i=1:2; for j=1:3; x(:,j)=[i+j+1;j*2 ]; end x(:,:,i)=x end
In this case, I want to have a result of 2*3*2 matrix, x(:,:,1) =
3 4 5
2 4 6
x(:,:,2) =
4 5 6
2 4 6
However, it turns out I only get
x(:,:,1) =
4 5 6
2 4 6
x(:,:,2) =
4 5 6
2 4 6
I know the problem is the x(:,:,i)=x line as x gets updated and renewed for each loop and covered by the new data again.
But I couldn't assign new name for building this matrix.
Can anyone help

채택된 답변

Thomas
Thomas 2012년 10월 30일
편집: Thomas 2012년 10월 30일
All you need to do is
for i=1:2;
for j=1:3;
x(:,j,i)=[i+j+1;j*2 ]; % add the 3rd dimension i
end;
end
P.S. It is good idea to refrain from using i & j as variable names since they are the complex function in MATLAB
  댓글 수: 2
Qian
Qian 2012년 10월 30일
thank you so much. That's what i've missed.
Gowthami Odeti
Gowthami Odeti 2017년 11월 9일
Thank you so much, works awesome!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by