Creating a for loop that calls to a separate function script and collects outputs in vector
1 次查看(过去 30 天)
显示 更早的评论
I have a function file called AirProperties.m that takes an Altitude and Mach Number as input and spits out 7 characteristics of the air in a row vector.
What I want to do is create a script that passes a vector of Altitude and a vector of Mach Number (of any size n) through AirProperties.m and have the output collected in a vector (n rows by 7).
Here is where I struggle. I want to evaluate AirProperties at the first Altitude value and every Mach Number, then the next Altitude value at every Mach Number.
This is what I have and it's not much:
% Inputs of Altitudes and Mach Numbers
n = 100; % Number of values
Alts = linspace(0,100000,n); % 100 values
Machs = linspace(0.5,5,n); % 100 values
% initialize vector to collect results
Results = zeros(n,7); % = [alt Me ReL Ue Cfe kadm Ra] The variables in the output of AirProperties.m
for i = 1:n
Results(i,:) = AirProperties(Alts(i),Machs(i));
for j = 1:n
Results(j,:) = AirProperties(Alts(j),Machs(j));
end
end
In this scenario, with 100 altitude values and 100 mach number values the size of the output vector would be 10000 by 7.
It's been a while and I know I'm missing some for loop basics in my brain, but a nudge in the right direction would be appreciated.
0 个评论
采纳的回答
Stephen23
2020-12-29
编辑:Stephen23
2020-12-29
One simple approach:
cnt = 0;
Results = nan(n*n,7); % preallocate 10000x7
for ii = 1:n
for jj = 1:n
cnt = cnt+1;
Results(cnt,:) = AirProperties(Alts(ii),Machs(jj));
end
end
It might make more sense to store the data in a 7xnxn array:
Results = nan(7,n,n); % preallocate 7x100x100
for ii = 1:n
for jj = 1:n
Results(:,ii,jj) = AirProperties(Alts(ii),Machs(jj));
end
end
Then the 2nd and 3rd dimensions trivially correspond to the Alts and Machs respectively.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Financial Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!