Lab changes
This commit is contained in:
51
Classes/Warehouse_class/classes/Parameter2.m
Normal file
51
Classes/Warehouse_class/classes/Parameter2.m
Normal file
@@ -0,0 +1,51 @@
|
||||
classdef Parameter2 < handle
|
||||
% PARAMETER2: Represents a physical parameter with mappings between values and indices
|
||||
|
||||
properties
|
||||
name
|
||||
values
|
||||
length
|
||||
physToIndexMap % Rename this from 'getPhysForIndex'
|
||||
indexToPhysMap % Rename this from 'getIndexForPhys'
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = Parameter2(name, values)
|
||||
% Constructor to initialize the Parameter2 object
|
||||
obj.name = name;
|
||||
obj.values = values;
|
||||
obj.length = numel(values);
|
||||
|
||||
% Initialize the mappings
|
||||
obj.physToIndexMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
|
||||
obj.indexToPhysMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
|
||||
obj = obj.buildMappings();
|
||||
end
|
||||
|
||||
function obj = buildMappings(obj)
|
||||
% Build mappings between physical values and indices
|
||||
for idx = 1:obj.length
|
||||
obj.indexToPhysMap(idx) = obj.values(idx);
|
||||
obj.physToIndexMap(obj.values(idx)) = idx;
|
||||
end
|
||||
end
|
||||
|
||||
function physVal = getPhysForIndex(obj, idx)
|
||||
% Return the physical value corresponding to the index
|
||||
if isKey(obj.indexToPhysMap, idx)
|
||||
physVal = obj.indexToPhysMap(idx);
|
||||
else
|
||||
error('Index out of range for parameter %s', obj.name);
|
||||
end
|
||||
end
|
||||
|
||||
function idx = getIndexByPhys(obj, physVal)
|
||||
% Return the index corresponding to the physical value
|
||||
if isKey(obj.physToIndexMap, physVal)
|
||||
idx = obj.physToIndexMap(physVal);
|
||||
else
|
||||
error('Physical value %g not found in parameter %s', physVal, obj.name);
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user