Getting vertcat error, not using vertcat

4 次查看(过去 30 天)
Error using vertcat Dimensions of matrices being concatenated are not consistent.
I keep getting this error despite not having vertcat in my code. What can I do to fix this? Code is attached

采纳的回答

John D'Errico
John D'Errico 2018-3-15
编辑:John D'Errico 2018-3-15
Your code:
ID=5415;
K=ID/1000;
uts=1;
uph=0.5;
a=0.05;
b=1.5;
for n=1:100
k5(n)=a+(b-a)*rand(size(n));
A=[-1 1 1 -1 0;-1 1+K 0 0 0;K 0 K*K (K*K)/(1+(K/K)) 1+K-K;0 0 0 K*(K+(K/(1+(K/K)))) -1;0 0 -1 1 k5/K];
B=[0;K;uts+uph;0;0];
x=inv(A)*B;
end
So, look at the LAST row of A. It is:
0 0 -1 1 k5/K
But what is k5?
k5(n)=a+(b-a)*rand(size(n));
After the very first iteration of that loop, you keep inserting new elements at the end of to k5, growing it. k5 is a vector.
That last row of A is your problem. What you really intended, how can I possible guess? n is a SCALAR. So rand(size(n) is ALWAYS just a scalar. Regardless, k5 is a vector, growing in length. So after the first iteration of your loop, k5 is a vector of length 2. Therefore, at iteration 2, the row:
0 0 -1 1 k5/K
now has length 6. NOT 5 as you so strongly claimed. On the nth iteration, that row would have length 4+n, that is, IF you could ever have gotten past n=2.
By the way, you ARE using vertcat. You may not know it, but you are. In fact, you use vertcat TWICE in that loop.
When you use square brackets with a semi-colon between elements or between rows, that is a call to vertcat.

更多回答(1 个)

Mark McBroom
Mark McBroom 2018-3-14
The assignment to A vertical concatentates 4 row vectors. The first 3 rows have 5 elements, the 4th row only has 4 elements.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by