ERROR USING VERTCAT HELP PLEASE

4 views (last 30 days)
David
David on 14 Jul 2014
Commented: James Tursa on 14 Jul 2014
I keep getting this error
"Error using vertcat Dimensions of matrices being concatenated are not consistent."
P=(100:100:1000);
for i=1:P
D = [1 0 0 -0.857 -0.832 -0.812;
0 1 0 0.286 0.555 0.542;
0 0 1 0.429 0 -0.217;
0 0 0 0 0 0.0379;
0 0 0 -0.1286 0 0.1218;
0 0 0 0.0857 0.1664 0.1625;];
E= [0;
P;
0;
0.15.*P;
0;
0.3.*P;];
X=D\E;
end
  2 Comments
José-Luis
José-Luis on 14 Jul 2014
Please type in your code instead of pasting an image. It would make it easier for people to help you.
David
David on 14 Jul 2014
Ok, i did that.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 14 Jul 2014
Edited: James Tursa on 14 Jul 2014
It is unclear what you are trying to do with the loop. P is a vector [100 200 ... 1000], so the for loop indexing i=1:P doesn't seem to make sense. And then inside your matrix building you have scalars mixed in with this P vector for the concatenation, hence the error. Did you mean to do this instead?
P=(100:100:1000);
for k=P
D = [1 0 0 -0.857 -0.832 -0.812;
0 1 0 0.286 0.555 0.542;
0 0 1 0.429 0 -0.217;
0 0 0 0 0 0.0379;
0 0 0 -0.1286 0 0.1218;
0 0 0 0.0857 0.1664 0.1625;];
E= [0;
k;
0;
0.15.*k;
0;
0.3.*k;];
X=D\E;
end
  2 Comments
David
David on 14 Jul 2014
This works thank you so much! i guess i was just following a template on my professors example problem (bad idea).
The only thing is that when i run the program the X only gives me one value rather than the 10 that are in (100:100:1000).
James Tursa
James Tursa on 14 Jul 2014
Option 1) Preallocate X and save each iteration in X, e.g.
P=(100:100:1000);
X = zeros(6,numel(P));
m = 1;
for k=P
D = [1 0 0 -0.857 -0.832 -0.812;
0 1 0 0.286 0.555 0.542;
0 0 1 0.429 0 -0.217;
0 0 0 0 0 0.0379;
0 0 0 -0.1286 0 0.1218;
0 0 0 0.0857 0.1664 0.1625;];
E= [0;
k;
0;
0.15.*k;
0;
0.3.*k;];
X(:,m)=D\E;
m = m + 1;
end
Option 2) Do all of the calculation in one fell swoop without a loop. E.g.,
P=(100:100:1000);
D = [1 0 0 -0.857 -0.832 -0.812;
0 1 0 0.286 0.555 0.542;
0 0 1 0.429 0 -0.217;
0 0 0 0 0 0.0379;
0 0 0 -0.1286 0 0.1218;
0 0 0 0.0857 0.1664 0.1625;];
E = zeros(6,numel(P));
E(2,:) = P;
E(4,:) = P*0.15;
E(6,:) = P*0.3;
X=D\E;

Sign in to comment.

More Answers (0)

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!