3 views (last 30 days)
Show older comments
Nikolas Nikola on 9 Jul 2011
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/11225-ascending-order-room-modes
Accepted Answer: Oleg Komarov
Open in MATLAB Online
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
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Oleg Komarov on 9 Jul 2011
Open in MATLAB Online
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(:)])
2 Comments Show NoneHide None
Show NoneHide None
Nikolas Nikola on 9 Jul 2011
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/11225-ascending-order-room-modes#comment_24550
Thanks for your response. Answering to your last question. Yes, at the end I want all the variables sorted together.
I haven't check yet the code you have sent. When I do i will answer properly. However, I think that my results are correct with my way, as well. It is not an efficient way though. I will check it again.
I made this quick response in order to answer to your last question.
Thanks again!
Nikolas Nikola on 9 Jul 2011
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/11225-ascending-order-room-modes#comment_24569
Thank you very much. That is what I need.
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLAB
Find more on MATLAB in Help Center and File Exchange
Tags
- ascendingoerder
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office