Create a 3D Matric

조회 수: 5 (최근 30일)
Lucas Rauscher
Lucas Rauscher 2017년 11월 15일
댓글: Stephen23 2017년 11월 15일
Hello,
I'd like to create a 3D matrix which changes for each loop with the following code :
X=ones(2*n+1,1);
for t=1:3
B1=vertcat(zeros(n,1),X(n,1,t)*ones(n+1,1));
X(:,:,t+1)=B1(:,:,t);
end
And I keep having this mistake on the B2 line, saying the dimensions mismatch. I tried different things, but I don't understand why it doesn't work. Could anyone of you help me please ? Thanks a lot !
  댓글 수: 4
M
M 2017년 11월 15일
편집: M 2017년 11월 15일
I guess your problem is for t>1 when you're trying to access B1(:,:,t), as B1 is a vector. Why would you want to access third dimension of B1 ?
Stephen23
Stephen23 2017년 11월 15일
Lucas Rauscher's "Answer" moved here:
Ok I'm sorry I'll explain what I try to achieve : The aim is to get Xsuiv(:,:,t), from t=1 to 10. So I initialize Xsuiv(:,:,1)=ones(NN,1);. Then, to get Xsuiv(:,:,t+1) I need to get Bsuiv(:,:,t). And to get Bsuiv(:,:,t) I can calculate it with Xsuiv(:,:,t-1) And once I have Bsuiv(:,:,t) I can get Xsuiv(:,:,t+1).
Xsuiv(:,:,1)=ones(N*M,1);
for t=2:10
Bsuiv=vertcat(zeros(n,1),Tp*ones(n+1,1));
for k=1:m-1
Bsuiv=vertcat(Bsuiv,zeros(n+1,1));
for i=1:n-1
%ERROR Bsuiv=vertcat(Bsuiv,((Xsuiv(k*N+n+i+1,1,t-1)-2*Xsuiv(k*N+n+i,1,t-1)+Xsuiv(k*N+n+i-1,1,t-1))
end
Bsuiv=vertcat(Bsuiv,Tp*ones(1,1));
end
Bsuiv=vertcat(Bsuiv,zeros(N,1));
Xsuiv(:,:,t+1)=eye(N*M,N*M)*Bsuiv(:,:,t);
end
Is it clear enough ? And one of my problems is to make Bsuiv a 3D matrix. For example in that case the line %ERROR is wrong because of the Xsuiv(...,...,t-1). But if I write
Bsuiv(:,:,t)=vertcat(Bsuiv,((Xsuiv(k*N+n+i+1,1,t-1)-2*Xsuiv(k*N+n+i,1,t-1)+Xsuiv(k*N+n+i-1,1,t-1))
it's still wrong

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

답변 (1개)

Jan
Jan 2017년 11월 15일
You want a 3D array. Then define a 3D array:
X = ones(2*n+1, 1, 4);
for t = 1:3
B1 = vertcat(zeros(n,1), X(n,1,t) * ones(n+1, 1));
X(:,:,t+1) = B1;
end
It is strange, that you access X(n,1,t) before it was written. But because you have posted the failing code only, it is hard to guess, what you want to achieve. Add some explanations.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by