MATLAB: Ascending order-room modes

ascendingoerder

Hi all,
I created a script in order to calculate room modes using this formula : f = ( c/2 ) * ( sqrt( ((n/L))^2 + ((n/w))^2 + ((n/h))^2 ) )
L, w, h are the length,width and height and n:0,1,2,3,4 , c=speed of sound
So, I calculated in such a way that the different mode combinations are illustrated separately. Now, I want to put ALL the results that I found into one vector/matrix in ascending order. I tried to do something with the "sort" command but it couldn't work with all these matrices. Can someone help on this?
That's the code:
% Room Mode Calculator RC1
% f = ( c/2 ) * ( sqrt( ((nL/L))^2 + ((nw/w))^2 + ((nh/h))^2 ) )
clear
tic
c = 344 ; % speed of sound (m/s)
fL=zeros(1,9); % initialize vector


fw=zeros(1,9); % initialize vector
fh=zeros(1,9); % initialize vector
w= 2.20 ;
L= 3.50 ;
h= 3.02 ;
% AXIAL MODES
for n = 1: 9
fL(n) = (c/2) * (n/L) ;
fw(n) = (c/2) * (n/w) ;
fh(n) = (c/2) * (n/h) ;
end
% Tangential modes
fLw = zeros(1,4);
fwh = zeros(1,4);
fLh = zeros(1,4);
for nL= 1:4
for nw = 1:4
fLw(nL,nw) = ( c/2 ) * ( sqrt( ((nL/L))^2 + ((nw/w))^2 )) ;
for nh = 1:4
fwh(nw,nh) = ( c/2 ) * ( sqrt( ((nw/w))^2 + ((nh/h))^2 )) ;
fLh(nL,nh) = ( c/2 ) * ( sqrt( ((nL/L))^2 + ((nh/h))^2 )) ;
end
end
end
% Oblique Modes
fLwh = zeros(1,2);
for nL= 1:2
for nw = 1:2
for nh = 1:2
fLwh(nL,nw,nh) = ( c/2 ) * ( sqrt( ((nL/L))^2 + ((nw/w))^2 + ((nh/h))^2 ) ) ;
end
end
end
' AXIAL MODES '
fL
fw
fh
'TANGENTIAL MODES'
fLw
fLh
fwh
'OBLIQUE MODES'
fLwh
toc
So, how can I put all the results into one vector or matrix and place them in ascending order? I always have problems with matlab and most of the times with easy things. I can not understand well its way of "thinking"..
Thanks in advance,
Aris

Best Answer

Your code can be vectorized to:
c = 344; % speed of sound (m/s)
w = 2.20;
L = 3.50;
h = 3.02;
% AXIAL MODES
n = 1:9;
fL = c/2 .* n/L;
fw = c/2 .* n/w;
fh = c/2 .* n/h;
% Tangential modes
nL = 1:4;
nw = 1:4;
nh = 1:4;
fLw = c/2 * sqrt(bsxfun(@plus, (nL.'/L).^2, (nw/w).^2));
fwh = c/2 * sqrt(bsxfun(@plus, (nw.'/w).^2, (nh/h).^2));
fLh = c/2 * sqrt(bsxfun(@plus, (nL.'/L).^2, (nh/h).^2));
% Oblique Modes
fLwh = c/2 * sqrt(bsxfun(@plus,(fLw(1:2,1:2) * 2/c).^2, permute(nh(1:2)/h,[3 1 2]).^2));
  • In your previous code you didn't preallocate correctly for the tangential modes and oblique ones.
  • Also the fLh depends on nL and nh but its calculation is nested into the loop for nw as well, which means that you calculate it several times redundantly overwrtiting the same results.
Now the MAIN question:
- Do you want fL,fw,fh,fLw,fwh,fLh and fLwh all together sorted?
EDIT
[srt, idx] = sort([fL(:);fw(:);fh(:);fLw(:);fwh(:);fLh(:);fLwh(:)])