Getting vertcat error, not using vertcat
7 views (last 30 days)
Show older comments
Andrew Tyers
on 14 Mar 2018
Edited: John D'Errico
on 15 Mar 2018
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
0 Comments
Accepted Answer
John D'Errico
on 15 Mar 2018
Edited: John D'Errico
on 15 Mar 2018
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.
0 Comments
More Answers (1)
Mark McBroom
on 14 Mar 2018
The assignment to A vertical concatentates 4 row vectors. The first 3 rows have 5 elements, the 4th row only has 4 elements.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!