Why can't I concatenate simple matrix? vertcat Dimensions of matrices being concatenated are not consistent
1 view (last 30 days)
Show older comments
I have the following 'simple' code whereby I make 6 complex numbers which are defined by a vector. I then want to stack these numbers in a matrix. It seems to me that if I define each number as a variable I can simply stack the variable in matrix C but if I try and just directly input them from a calculation then I can't stack the numbers:
clear
A = [1,2,3,4,5,6];
x = 2;
w0 = 2*pi*67*10^9;
wj = 2*pi*86*10^9;
wp = 2*pi*12*10^9;
maxBeta = 0.45;
wsfac = 0.6;
wifac = 1-wsfac;
ws = wsfac*wp;
wi = wifac*wp;
wshg = 2*wp;
wps = wp+ws;
wpi = wp+wi;
Ap0 = 0.5*w0/wp;
As0 = Ap0*sqrt(0.0057*wp/ws);
Ashg0 = 0;
Aps = 0;
Spi = 0;
kp = (wp/w0)*(1/(sqrt(1-(wp/wj)^2)));
ks = (ws/w0)*(1/(sqrt(1-(ws/wj)^2)));
ki = (wi/w0)*(1/(sqrt(1-(wi/wj)^2)));
kshg = (wshg/w0)*(1/(sqrt(1-(wshg/wj)^2)));
kps = (wps/w0)*(1/(sqrt(1-(wps/wj)^2)));
kpi = (wpi/w0)*(1/(sqrt(1-(wpi/wj)^2)));
delk = 3*ws*wi*wp/(2*w0*(wj^2));
modk = sqrt(wp*Ap0^2/(ws*As0^2+wp*Ap0^2));
dp = -(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
ds = (maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
di = (maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
dshg = -(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x) -(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
dps = -(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
dpi = -(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x);
B = [dp
ds
di
dshg
dps
dpi]
D = [-(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
(maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
(maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
-(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x) -(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
-(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
-(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x)];
I don't understand why MATLAB can stack the variables but not the calculated values which are the same.
This is for use in a coupled differential equation solver whereby A(1:6) are unknown and thus defining variables initially is not an option.
Pre-thanks for any help
Tom
2 Comments
Accepted Answer
Matt J
on 13 Sep 2018
You have a stray space in your expression for the 4th entry of D. This makes Matlab see two entries instead of one. Copy over it with the following,
D = [-(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
(maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
(maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
-(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x)-(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
-(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
-(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x)];
6 Comments
More Answers (0)
See Also
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!