Error using vertcat Dimensions of matrices being concatenated are not consistent.
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Guillaume
am 1 Nov. 2017
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.
Antworten (3)
M
am 1 Nov. 2017
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 ?
Guillaume
am 1 Nov. 2017
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 Kommentare
Guillaume
am 2 Nov. 2017
Bearbeitet: Guillaume
am 8 Nov. 2017
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.
edit: I missed that Zp is also a vector.
6 Kommentare
Guillaume
am 9 Nov. 2017
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.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!