Error using vertcat Dimensions of matrices being concatenated are not consistent.

Hi all, I need help to solve the matrix calculation, multiplying matrix e and q.Both matrices are 2x2 matrix.But there is still an error.I'm attaching my code - SAC_SMPP.m being is script file and yes.m is my matrix file.
here is the error line;
Error using vertcat Dimensions of matrices being concatenated are not consistent.
Error in yes (line 2) e =[1, Zt; 0, 1];
Error in SAC_TMM (line 102) Ttotal = yes(Zt,kc,Zp,h);%call in yes.m file

2 Comments

What is size(Zt) when you call your yes function. You'll find that it is not 1x1 which your code assumes.
Unfortunately, you haven't provided the code for SAC_TMM.m, so we have no idea why your Zt is not scalar.
my assumption is my Zt value is 1x1, it consist of real and imaginary value. attached the SAC_TMM file

Sign in to comment.

Answers (3)

You're trying to concatenate a matrix [1 Zt] which is of dimension 1*602 with a matrix [0 1] which is of dimension 1*2.
PS : What is the file SAC_TMM ?
my assumption is my Zt value is 1x1
I don't understand why you assume that. Zt depends on a bunch of variables that all depends on w that depends on f which is defined as a vector. Thus Zt is going to be the same size as f (i.e. 1x801).
Rather than assuming, do actually check what is happening with your code. Use the debugging tools of matlab to step through your code line by line and check that it produces the result you expect. Issuing dbstop if error before running your code will also helps as matlab will break into the debuggers as soon as it encounters an error, so you can look at what your inputs actually are.
I've not tried to understand what it is you are calculating but the code from that Ttotal line does not make any sense if you are expecting Ttotal to be a 2x2 matrix, since at the end you are trying to plot some 2x2 matrix derived from Ttotal against the vector f. The only way the plot would work is if Ttotal is the same size as f, so your strangely named yes function ought to return something the same size as Zt, not something 2x2.

4 Comments

I am sorry for the misunderstood. The size of Zt is (1x801) as the frequency range is (f= 200:2:1800;).
after the calculation of Zt, the value of Zt are used to be one of the component in matrix e(2x2). e = [1, Zt; 0, 1].
next, matrix e will be multiply with matrix q. Ttotal =(e.*q);
then, i call back the yes file into SAC_TMM to calculate the alfa_final Zs = Ttotal; z1 = (Zs-1); z2 = (Zs+1); z = z1./z2;
alfa_final = 1-(abs(z)).^2;
i hope you get the idea what i am trying to do. tq
No, I don't understand. As you say and as I've pointed out, Zt is 1x801.
So, first question, what is the value of Zt that is supposed to go in your e matrix? Zt contains 801 values, not one.
Then you're saying that your e matrix is going to be 2x2 and do a bunch of calculation on that. You're thus going to end up with a 2x2 matrix. You then try to plot that. I have no idea what the plot of a 2x2 matrix is to you. And in any case, trying to plot it against your f vector is never going to succeed.
the value of Zt is 1x801. then the value of Zt will go in to matrix e. for each value of Zt will go into matrix e. means i will have 801 matrix e with each value of Zt. after that matrix e multiply with matrix q = Ttotal. Ttotal will also 1x801 in matrix form 2x2.
"Then you're saying that your e matrix is going to be 2x2 and do a bunch of calculation on that. You're thus going to end up with a 2x2 matrix. You then try to plot that. I have no idea what the plot of a 2x2 matrix is to you. And in any case, trying to plot it against your f vector is never going to succeed." I get your point here, i will check again the formula.
I knew which part that I got wrong. "the value of Zt is 1x801. then the value of Zt will go in to matrix e. for each value of Zt will go into matrix e. means i will have 801 matrix e with each value of Zt. after that matrix e multiply with matrix q = Ttotal. Ttotal will also 1x801 in matrix form 2x2."
the part of the formula that I missed is, after the calculation of matrix e and matrix q=Ttotal. the Ttotal is matrix of 2x2.
Zs = Ttotal(1)./Ttotal(3); each value of Ttotal of row 1 column 1 will be divided with row two column 1. then i will be able to plot graph against f.
the problem is how i am going to multiply matrix e with matrix q with each value of Zt in the matrix e? matrix e will multiply with matrix q 801 times because Zt have 801 value.

Sign in to comment.

If you want to apply your yes formula to each element of Zt then you have to use a loop, or arrayfun.
Using arrayfun:
Ttotal = arrayfun(@(v1, v2) yes(v1, kc, v2, h), Zt, Zp, 'UniformOutput', false); %creates a 1x801 cell array of 2x2 matrices
Ttotal = cat(3, Ttotal{:}); %convert cell array into 2x2x801 matrix
Zs = reshape(Ttotal(1, 1, :) ./ Ttotal(2, 1, :), 1, []);
Using a loop:
Zs = zeros(size(Zt));
for idx = 1:numel(Zt)
Ttotal = yes(Zt(idx), kc, Zp(idx), h);
Zs(idx) = Ttotal(1) / Ttotal(3);
end
However, note that you're using memberwise operations in all your calculations. I suspect that it is an error. Because as it is, half of the elements of your e and q are not used and there is no point in constructing your matrices.
You have:
e = [1, Zt; 0, 1];
q = [q1, q3; q2, q4]; %values don't matter
Ttotal = e .* q; %memberwise multiplication!
That last line is equivalent to:
Ttotal = [1*q1, Zt*q3; 0*q2, 1*q4];
Ttotal = [q1, Zt*q3, 0, q4];
So, if Zs = Ttotal(1) / Ttotal(3), then
Zs = q1 ./ (q3*Zt);
Zs = 1./Zt * q1/q3
So you could avoid the arrayfun or loop and the yes function and simply write:
Zs = 1./Zt * cos(kc*h)/(1i*Zp*sin(kc*h));
and be done with it.
See the difference of Array vs Matrix operations.
edit: I missed that Zp is also a vector.

6 Comments

thank you for the response, really appreciate it. but i still couldn't get the plotted graph as expected. i've figure out the mistake that i made.
e = [1, Zt; 0, 1]; q = [q1, q3; q2, q4]; %values don't matter Ttotal = e .* q; %memberwise multiplication!
it is not a memberwise multiplication, it is a matrix multiplication. thus i need to write it this way, isn't it?
Ttotal=e.q; %%error = Inner matrix dimensions must agree.
but, i did got an error as above. i've the calculation equation
Yes, I very much suspected that you didn't want memberwise multiplication. You better go over every single equation in your code to make sure that you haven't made the same mistake in other parts.
The actual code should be
Ttotal = e * q; %no dot
There is no way that the above will error if e and q are both 2x2. So what is the size of e and q when the error occurs? You must have changed how they are constructed.
below are the size of my e and q
e = 2 x 1602
g = 2 x 1602
the value of row and column became double,
as in my yes file
e =[ones(1,801) Zt; zeros(1,801) ones(1,801)];
q =[cos(kc.*h) 1i.*Zp.*sin(kc.*h); 1i.*sin(kc.*h)./Zp cos(kc.*h)];
Ttotal =e*q;
matrix e(2x2) x matrix q(2x2), exq=(2x2 matrix)
both matrix are 2x2 matrix, but i still couldn't solve it, really appreciate for your help, tq
the error as below
Error using *
Inner matrix dimensions must agree.
Error in yes (line 4)
Ttotal =e*q; %without dot
Error in SAC_TMM (line 102)
Ttotal = yes(Zt,kc,Zp,h);%call in yes.m file
I really do not understand why you state "matrix e(2x2) x matrix q(2x2), exq=(2x2 matrix)" when you clearly have declared them as 2x1602 matrices.
As per my answer, you need to loop over the elements of Zt (and Zp) either using an explicit loop or using arrayfun, before calling your yes function.
I have never shown any code where you declared e as a 2x1602 matrix, so I have no idea where that comes from.
this is the size of my matrix e and q after run the coding; as below
e = 2 x 1602 (it means that it have 2 row and 1602 column set of data) g = 2 x 1602 (it means that it have 2 row and 1602 column set of data)
the expected size data of matrix of e and q that i should get is 4x801,(it means that it have 4 row and 801 column set of data)
because i have four element in matrix e and q with frequency range from 200:2:1800, so i 'll have 801 set of each matrix e and q respectively.
the multiplication of matrix e and q will resulted 4x801(it means that it have 4 row and 801 column set of data), but there is error in multiplication of matrix e and q, both have the same size.
Error using * Inner matrix dimensions must agree. Error in yes (line 4) Ttotal =e*q; %without dot Error in SAC_TMM (line 102) Ttotal = yes(Zt,kc,Zp,h);%call in yes.m file
i am sorry for the misunderstood of the term used to explain the size etc.tq
You need to pause and reflect on what you are doing. Your statements don't make much sense.
The rule of mathematics are such that you cannot multiply a 2x1602 matrix by a 2x1602. This is maths, you can't change it and it doesn't make sense. In any case, multiplying two matrices together will never result in "801 set of each matrix e and q respectively".
As I keep telling your, you need to loop over the elements of you Zt and Zp vectors. For each element, you create your 2x2 matrices and multiply them together. Get your Ttotal for each element and build a new vector out of that.
Code to do that is provided in my initial answer post. You just need to modify your original yes, that creates 2x2 matrices, to change the .* to *.
I never suggested to create 2x1602 matrices or 4x801 matrices or whatever. That is never going to work.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 1 Nov 2017

Commented:

on 9 Nov 2017

Community Treasure Hunt

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

Start Hunting!