Create a 3D Matric

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 comentarios

M
M el 15 de Nov. de 2017
What do you want to do exactly ?
X should be a 3D matrix ? here you are defining vectors.
Lucas Rauscher
Lucas Rauscher el 15 de Nov. de 2017
Editada: Lucas Rauscher el 15 de Nov. de 2017
I'd like to implement my X matrix at the time t+1, according to the B matrix at the time t.
X(:,:,4)=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
In that case X is a 3D matrix, but I still have a "index exceeds matrix dimensions" on this line :
X(:,:,t+1)=B1(:,:,t);
M
M el 15 de Nov. de 2017
Editada: M el 15 de Nov. de 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 el 15 de Nov. de 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

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 15 de Nov. de 2017

0 votos

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.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Nov. de 2017

Comentada:

el 15 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by