Build matrix from an unknown number of parameters
1 次查看(过去 30 天)
显示 更早的评论
Consider a model study which varies several different parameters, for example a study of building temperature in which parameters such as insulation value are varied. Each parameter's value is swept through a chosen range, resulting in a separate model run and output file for every possible combination of parameter values. From the output time series, metrics like the time averaged temperature can be calculated. To summarize the study, we want to store the average temperature vs. parameter choice in a matrix, but here's the problem: the number of parameters and their values differs every time the study is performed. One study might vary occupancy, insulation, and latitude; the next might vary number of windows and window glass type. Can you correct the following code so that it builds a summary matrix for an unknown number of parameters and their values?
% Parameters/values used in this study; not the same in subsequent studies
parameterList(1).name = 'occupancy' ;
parameterList(1).value = 0:10:100 ; % Percent of building occupied
parameterList(2).name = 'insulation' ;
parameterList(2).value = 1:6 ; % Insulation value
parameterList(3).name = 'latitude' ;
parameterList(3).value = 20:20:60 ; % Latitude of building site
fileList = dir('*.mat') ; % List of model output files; 198 for this study
function StudySummary = get_average_temperature(parameterList, fileList)
for iFile = 1:length(fileList)
% Each mat file contains the simulation's parameter values and output
load(fileList{iFile}) ;
% How to assign average temperature from this run to a summary matrix?
StudySummary.averageTemperature(iParameter1, iParameter2, ...) = ...
mean(temperatureTimeSeries) ;
end
0 个评论
采纳的回答
Walter Roberson
2012-7-6
Is it necessary to do the search based upon parameter values? As in the user gives a query "tell me the information for occupancy 50, insulation 4, latitude 40" ? Or is the summary to include all available information?
If you are building a summary of all available information, then just use indexing, such as
averageTemperature{6, 4, 2}
You can prepare these indices ahead of time without the retrieval needing to know how many there are:
idx = [6, 4, 2];
cellidx = num2cell(idx);
averageTemperature{ cellidx{:} }
If you are working off of a query then you need to worry about the query values not exactly matching the known parameter values. You need to find the nearest known parameter value. Such as
paramidx = interp1( paramvalues{1}, 1:length(paramvalues{1}), QueryValue, 'nearest');
and repeat that for each query parameter to arrive at the idx array that you plug into the scheme above.
更多回答(1 个)
Aaditya Kalsi
2012-7-6
I think what you're looking for is cells.
You can store up all values in a cell as:
a = rand(10);
b = ones(15);
c = 5;
paramVals = {a, b, c};
You can then use this to pass each one of the data values in each cell by:
f(a, b, c) can be replaced by f(paramVals{:})
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!