Create a 3D Matric

5 views (last 30 days)
Lucas Rauscher
Lucas Rauscher on 15 Nov 2017
Commented: Stephen23 on 15 Nov 2017
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 Comments
M
M on 15 Nov 2017
Edited: M on 15 Nov 2017
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 on 15 Nov 2017
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

Sign in to comment.

Answers (1)

Jan
Jan on 15 Nov 2017
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!