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 个评论
M
2017-11-15
What do you want to do exactly ?
X should be a 3D matrix ? here you are defining vectors.
Lucas Rauscher
2017-11-15
编辑:Lucas Rauscher
2017-11-15
Stephen23
2017-11-15
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
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!