How to find which equation produced a specific answer in a for loop?

I've used a for loop to calculate modal frequencies of a room.
close all;
clear all;
Lx = 8; % Set room dimensions (metres)
Ly = 5;
Lz = 4;
c = 343; % Speed of sound
num_mx = 10; % End modenumbers to calculate
num_my = 10;
num_mz = 10;
i=0;
for mx = 0:num_mx % First we need to generate a vector of modal frequencies
for my = 0:num_my
for mz = 0:num_mz
i=i+1;
freq(i) = c*sqrt((mx/(2*Lx))^2 + (my/(2*Ly))^2 + (mz/(2*Lz))^2);
% frequency of mode
end % mz
end % my
end % mx
fsorted = sort(freq); % sort the modal frequencies
fsorted(2:22) % display 20 lowest modes
I need to find which values of mx, my and mz produced the 20 lowest values i've sorted at the end but can't figure out how, any help would be appreciated.

 Accepted Answer

Ideally you would be able to tell from its location in the array. That information gets jumpled somewhat when you sort, but if you use this syntax, it will return the original index value.
However, the simplest way is probably to capture the values of mx, my and mz in as well. Then you'd have the values right next to the frequencies.
freq(i,1) = c*sqrt((mx/(2*Lx))^2 + (my/(2*Ly))^2 + (mz/(2*Lz))^2);
freq(i,2:4) = [mx,my,mz];
You'll want to use sortrows instead of sort if you do this.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!