Need Help with fixing "Error using vertcat Dimensions of matrices being concatenated are not consistent" for my matrix

1 view (last 30 days)
Hello All,
I am in need of some help, I am trying to store results where P1, P2, and P3 are dependent on J in matrix B however I keep getting the the vertcat error message. I believe it may have something to do with storing several matrices in one matrix but I don't really know how to get around this since I need to create a matrix for every value of J.
j = 0:10;
B = [0;(P1+P2+P3);(P1+P2+P3);P1;0;0;0;0;0;P3;0;P2];
Any help would be appreciated!
Thank you for your time.
CAAJ

Accepted Answer

John BG
John BG on 10 Feb 2017
another way would be (let me call j now j0)
syms j0
P2(j0)=3*j0^2-2*j0+.5
P2(j0) =
3*j0^2 - 2*j0 + 1/2
>> P3(j0)=(8-j0^2).^5
P3(j0) =
-(j0^2 - 8)^5
B = [0;(P1+P2+P3);(P1+P2+P3);P1;0;0;0;0;0;P3;0;P2]
B(j0) =
0
3*j0^2 - (j0^2 - 8)^5 - j0 + 3/2
3*j0^2 - (j0^2 - 8)^5 - j0 + 3/2
j0 + 1
0
0
0
0
0
-(j0^2 - 8)^5
0
3*j0^2 - 2*j0 + 1/2
now you change j0 from symbolic to double
j0=[0:1:10]
=
0 1 2 3 4 5 6 7 8 9 10
and translate symbolic B to a cell
C=sym2cell(B)
you can still read the symbolic expression with
C{1}
ans(j0) =
0
3*j0^2 - (j0^2 - 8)^5 - j0 + 3/2
3*j0^2 - (j0^2 - 8)^5 - j0 + 3/2
j0 + 1
0
0
0
0
0
-(j0^2 - 8)^5
0
3*j0^2 - 2*j0 + 1/2
but you have all numeric values, individually
single(C{1}(4))
ans =
1.0e+04 *
0
-3.2722500
-3.2722500
0.0005000
0
0
0
0
0
-3.2768000
0
0.0040500
or read all values to for instance write them in another variable
for k=1:1:numel(j0)
double(C{1}(k))
end
=
1.0e+04 *
0
1.681050000000000
1.681050000000000
0.000200000000000
0
0
0
0
0
1.680700000000000
0
0.000150000000000
=
1.0e+03 *
0
1.035500000000000
1.035500000000000
0.003000000000000
0
0
0
0
0
1.024000000000000
0
0.008500000000000
..
=
1.0e+10 *
0
-1.842435143950000
-1.842435143950000
0.000000001200000
0
0
0
0
0
-1.842435179300000
0
0.000000034150000
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

More Answers (2)

James Tursa
James Tursa on 10 Feb 2017
Not sure what P1, P2, and P3 dimensions are, but maybe something like this is what you need:
row0 = zeros(1,size(P1,2));
B = [row0;(P1+P2+P3);(P1+P2+P3);P1;row0;row0;row0;row0;row0;P3;row0;P2];
  7 Comments
James Tursa
James Tursa on 10 Feb 2017
Display in what way? For one thing, your j variable has 11 elements, but your B variable has 12 rows. So not sure how they correlate to each other.
CAAJ
CAAJ on 11 Feb 2017
Edited: CAAJ on 11 Feb 2017
I would like to see matrix B(the 12x1 matrix) after every iteration of J(0 to 10). So essentially when J=1, it would result in a different matrix than J=2. Because P1=0, P2=(.1*J)*1000 and P3=(1-(.1*J))*1000.
Hopefully this clarifies this situation.
Thanks again

Sign in to comment.


CAAJ
CAAJ on 11 Feb 2017
Hello All,
Thank you all for your assistance but I have actually found a way to solve and display the results I needed. I ended up creating a function to solve the B matrix for the 10 different iterations. See the code below.
Created Function:
function [B]= Result_B(P0,P1)
for j = 1:10;
P3=(1-(.1*j))*P0;
P2=(.1*j)*P0;
B(:,:,j) = [0;(P1+P2+P3);(P1+P2+P3);P1;0;0;0;0;0;P3;0;P2];
end
In the command window:
B = Result_B(P0,P1)
B(:,:,j) %to see the resulting matrix from the jth iteration

Community Treasure Hunt

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

Start Hunting!