Multiplying a changing amount matrices.

the problem I am struggeling with is to multiply a known, but depending on the application, changing amount of matrices (2x2). I would like to write a code that does this without me having to manually enter the matrix names. It would work like horzcat() but for a different amount of matrices every time i use the program. Currently this is what it should do:
for i_freq=1:length(freq) omega=2*pi*freq(i_freq); k=omega/c_0;
%define muffler elements
T1=straight_duct(k,L1);
T2=expansion_duct(E);
T3=straight_duct(k,L2);
T4=contraction_duct(E);
T5=straight_duct(k,L3);
%Transfer transmission matrix
TT=T5*T4*T3*T2*T1;
%extract elements from matrix
A=TT(1,1)
B=TT(1,2)
C=TT(2,1)
D=TT(2,2)
F=A-C/D;
T=[A B; C D];
%Transmission loss
TL(i_freq)=20*log10(abs(F));
end
I'm pretty new to matlab so a shove in the right direction would be nice. I've been looking at plenty of topics but none have actually resolved this problem for me (dynamic matrices, dynamic variables, recognizable variables). The order of the multiplication matters.
hope some one can show me the light with which i can continue working!
cheers.

 Respuesta aceptada

Youri
Youri el 22 de Nov. de 2013

0 votos

Thank you very much for your reply, the 3D matrix was something that is new to me and i am currently using. It's an awesome way of saving our arrays. The problem I'm still having is the product, I will have to think of a different way of resolving this issue.

4 comentarios

Simon
Simon el 22 de Nov. de 2013
You can create arrays of (almost) arbitrary dimensions, but sometimes it gets confusing using more than 3 dimensions ;-)
What are the problems with the product?
Youri
Youri el 25 de Nov. de 2013
Editada: Youri el 25 de Nov. de 2013
% T(1:2, 1:2, 1)=straight_duct(k,L1); %call straight duct T(1:2, 1:2, 2)=expansion_duct(E); %call expansion duct T(1:2, 1:2, 3)=straight_duct(k,L2); T(1:2, 1:2, 4)=contraction_duct(E); %call contraction duct T(1:2, 1:2, 5)=straight_duct(k,L3);
%Transfer transmission matrix
%TT=T(:,:,5)*T(:,:,4)*T(:,:,3)*T(:,:,2)*T(:,:,1); %what it should do
sizeT=size(T); %get size of 3D T matrix - returns an array
dimTT=sizeT(1,3); %take the number of matrices
TT=zeros(2,2); %initialize zero matrices
for i_matrix=dimTT:-1:1
if i_matrix==dimTT
TT=TT+T(:,:,i_matrix); %insert first matrix in
else
TT=TT*T(:,:,i_matrix); %multiply following matrices in
end
end
This is how I solved it. I wasn't sure why the prod command wasn't working like I wanted. This solution gives the right answer so I thought i'd share it :) the 3D matrix is super helpfull though!
Simon
Simon el 25 de Nov. de 2013
I'm sorry, I misunderstood the problem. You want to multiply the matrices, whereas I thought (and this is what "prod" does) you want to multiply the entries of the matrix.
Youri
Youri el 25 de Nov. de 2013
ah, sorry for the misleading info. Still new to all of this so the correct way of paraphrasing my questions is not a skill i have yet :) you pointed me in the right direction which was what i ultimately needed

Iniciar sesión para comentar.

Más respuestas (1)

Simon
Simon el 18 de Nov. de 2013
Hi!
I'm not sure if I understood correctly. You can concat N matrices into a 3d matrix with dimensions (2x2xN). Basically, you stack them together. Then you may use "prod" to take the product along the third dimension.
%define muffler elements
T(1:2, 1:2, 1)=straight_duct(k,L1);
T(1:2, 1:2, 2)=expansion_duct(E);
T(1:2, 1:2, 3)=straight_duct(k,L2);
T(1:2, 1:2, 4)=contraction_duct(E);
T(1:2, 1:2, 5)=straight_duct(k,L3);
%Transfer transmission matrix
TT=prod(T, 3);
Is this what you're looking for?

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de Nov. de 2013

Comentada:

el 25 de Nov. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by