MATLAB: Can I concatenate single column arrays into a matrix with multiple columns and scalars

concatenate

I need to solve a system of equations using a matrix. The problem is that individual indices are actually a range of values while some are scalars. My code is below. Within the matrix, M (7×7) the exponent terms contain the vector, k (1000×1). The error message when run is: "Error using horzcat: Dimensions of matrices being concatenated are not consistent." Should I attempt to solve this issue using another method, or is there a work around to store the info from the arrays within the matrix?
-thanks
a = 0.02; c = 343; rho = 0.9;
L = 0.33; L1 = 0.1;
S1 = 0.025; S2 = 0.07;
fmax = 1.84/pi*c/a;
f = transpose(linspace(0,fmax,1000));
omega = 2*pi.*f;
k = omega./c;
M = [1 0 0 0 0 0 0;
exp(-1i.*k.*L1) exp(1i.*k.*L1) -exp(-1i.*k.*L1) -exp(1i.*k.*L1) 0 0 0;
exp(-1i.*k.*L1) exp(1i.*k.*L1) 0 0 0 -1 -1;
S1.*exp(-1i.*k.*L1) -S1.*exp(1i.*k.*L1) -S1.*exp(1i.*k.*L1) S2.*exp(1i.*k.*L1) 0 (S2-S1) (S1-S2);
0 0 exp(-1i.*k.*L) exp(1i.*k.*L) exp(-1i.*k.*L) 0 0;
0 0 S2.*exp(-1i.*k.*L) -S2.*exp(1i.*k.*L) -S1.*exp(-1i.*k.*L) 0 0;
0 0 0 0 0 exp(1i.*k.*L1) -exp(-1i.*k.*L1)];
B = [1;0;0;0;0;0;0];
C = M\B;
tau = (norm(C(7)))^2/(norm(C(1)))^2;
TL = 10*log(tau^(-1));

Best Answer

Kevin
I have changed your code to avoid the matrix mismatch errors, but I am not sure what is it that you are after with these calculations, let me explain:
a = 0.02; c = 343; rho = 0.9;
L0 = 0.33; L1 = 0.1;
S1 = 0.025; S2 = 0.07;
fmax = 1.84/pi*c/a;
f = transpose(linspace(0,fmax,10)); % amount elements f reduced to 10 for test
omega = 2*pi.*f;
k = omega./c;
R1=ones(10,1)
R0=zeros(10,1)
M=zeros(7,7,10)
M(1,:,[1:10])=reshape([R1 R0 R0 R0 R0 R0 R0],[1,7,10])
M(2,:,[1:10]) =reshape( [exp(-1i.*k.*L1) exp(1i.*k.*L1) -exp(-1i.*k.*L1) -exp(1i.*k.*L1) R0 R0 R0],[1,7,10])
M(3,:,[1:10]) =reshape( [exp(-1i.*k.*L1) exp(1i.*k.*L1) R0 R0 R0 -R1 -R1],[1,7,10]);
M(4,:,[1:10])=reshape( [S1*exp(-1i.*k.*L1) , -S1*exp(1i.*k.*L1) ,-S1*exp(1i.*k.*L1) , S2*exp(1i.*k.*L1), R0 , (S2-S1)*R1 ,(S1-S2)*R1],[1,7,10]);
M(5,:,[1:10]) =reshape( [R0 R0 exp(-1i.*k.*L0) exp(1i.*k.*L0) exp(-1i.*k.*L0) R0 R0],[1,7,10]);
M(6,:,[1:10]) =reshape( [R0 R0 S2*exp(-1i.*k.*L0) -S2*exp(1i.*k.*L0) -S1*exp(-1i.*k.*L0) R0 R0],[1,7,10]);
M(7,:,[1:10]) =reshape( [R0 R0 R0 R0 R0 exp(1i.*k.*L1) -exp(-1i.*k.*L1) ],[1,7,10]);
B=[R1'; R0'; R0'; R0'; R0'; R0'; R0']
at this point there are 2 possible solution matrix C
1.- C is 7x7
nonzeros(linsolve(squeeze(M(:,1,:))',B'))
nonzeros(linsolve(squeeze(M(:,2,:))',B'))
nonzeros(linsolve(squeeze(M(:,3,:))',B'))
nonzeros(linsolve(squeeze(M(:,4,:))',B'))
nonzeros(linsolve(squeeze(M(:,5,:))',B'))
nonzeros(linsolve(squeeze(M(:,6,:))',B'))
nonzeros(linsolve(squeeze(M(:,7,:))',B'))
or 2.- 10x10
nonzeros(linsolve(squeeze(M(:,1,:)),B))
nonzeros(linsolve(squeeze(M(:,2,:)),B))
nonzeros(linsolve(squeeze(M(:,3,:)),B))
nonzeros(linsolve(squeeze(M(:,4,:)),B))
nonzeros(linsolve(squeeze(M(:,5,:)),B))
nonzeros(linsolve(squeeze(M(:,6,:)),B))
nonzeros(linsolve(squeeze(M(:,7,:)),B))
Obviously, this manual way can be shortened with for loops or other more compact commands.
Since I didn't build matrix C the last part of your code would be
tau1 = norm(nonzeros(linsolve(squeeze(M(:,7,:))',B')))^2/norm(nonzeros(linsolve(squeeze(M(:,1,:))',B')))^2
tau2 = norm(nonzeros(linsolve(squeeze(M(:,7,:)),B)))^2/norm(nonzeros(linsolve(squeeze(M(:,1,:)),B)))^2
tau1 =
0.851537512236636
tau2 =
3.619575696236079
and
TL1_log2 = 10*log(tau1^(-1))
TL2_log2 = 10*log(tau2^(-1))
TL1_log2 =
1.607117255495374
TL2_log2 =
-12.863568079834270
Bear in mind that in MATLAB log() is log base 2. to calculate log base 10 you should use log10()
TL1_log10 = 10*log10(tau1^(-1))
TL2_log10 = 10*log10(tau2^(-1))
TL1_log10 =
0.697962155833140
TL2_log10 =
-5.586576634658832
Kevin
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