Custom learner function for Adaboost - matlab

I am using Adaboost to fit a classification problem. We can do the following:
ens = fitensemble(X, Y, 'AdaBoostM1', 100, 'Tree')
Now 'Tree' is the learner and we can change this to 'Discriminant' or 'KNN'. Each learner uses a certain Template Object Creation Function. More info here.
Is it possible to create your own function and use it as a learner? And how?

I open templateTree.m and templateKNN.m to see how MATLAB define Template Object Creation Function.
function temp = templateKNN(varargin)
classreg.learning.FitTemplate.catchType(varargin{:});
temp = classreg.learning.FitTemplate.make('KNN','type','classification',varargin{:});
end
and
function temp = templateTree(varargin)
temp = classreg.learning.FitTemplate.make('Tree',varargin{:});
end
It shows that MATLAB has a function in FitTemplate named make , if you open this m-file, you will see :
function temp = make(method,varargin)
% Check the type of the required argument
if ~ischar(method)
error(message('stats:classreg:learning:FitTemplate:make:BadArgs'));
end
% Extract type (classification or regression)
args = {'type'};
defs = { ''};
[usertype,~,modelArgs] = ...
internal.stats.parseArgs(args,defs,varargin{:});
% Check usertype
if ~isempty(usertype)
usertype = gettype(usertype);
end
% Method
namesclass = classreg.learning.classificationModels();
namesreg = classreg.learning.regressionModels();
[tfclass,locclass] = ismember(lower(method),lower(namesclass));
[tfreg,locreg] = ismember(lower(method),lower(namesreg));
if ~tfclass && ~tfreg
error(message('stats:classreg:learning:FitTemplate:make:UnknownMethod', method));
end
if tfclass && tfreg
method = namesclass{locclass}; % can get it from namesreg too
type = usertype;
% If type is not passed for an ensemble method, try to
% figure it out from learner types. This is useful for
% users who want to type
% fitensemble(X,Y,'Subspace',100,'Discriminant')
% instead of
% fitensemble(X,Y,'Subspace',100,'Discriminant','type','classification')
if isempty(type) && ismember(method,classreg.learning.ensembleModels())
[learners,~,~] = internal.stats.parseArgs({'learners'},{},modelArgs{:});
if ischar(learners) || isa(learners,'classreg.learning.FitTemplate')
learners = {learners};
elseif ~iscell(learners)
error(message('stats:classreg:learning:FitTemplate:make:BadLearnerTemplates'));
end
L = numel(learners);
% The user can pass several learner templates, and some
% of these learners may be appropriate for
% classification, some for regression, and some for
% both. The ensemble type cannot be determined
% unambiguously unless if all learners are appropriate
% for one type of learning *only*. For example, in 12a
% t1 = ClassificationDiscriminant.template
% t2 = ClassificationKNN.template
% fitensemble(X,Y,'Subspace',10,{t1 t2})
% is going to work because both discriminant and k-NN
% can be used for classification only. If you want to
% mix discriminant and tree, you have to specify the
% ensemble type explicitly:
% t1 = ClassificationDiscriminant.template
% t2 = ClassificationTree.template
% fitensemble(X,Y,'Bag',10,{t1 t2},'type','classification')
types = zeros(L,1); % -1 for regression and 1 for classification
for l=1:L
meth = learners{l};
if isa(meth,'classreg.learning.FitTemplate')
meth = meth.Method;
end
isc = ismember(lower(meth),lower(namesclass));
isr = ismember(lower(meth),lower(namesreg));
if ~isc && ~isr
error(message('stats:classreg:learning:FitTemplate:make:UnknownMethod', meth));
end
types(l) = isc - isr;
end
if all(types==1)
type = 'classification';
elseif all(types==-1)
type = 'regression';
end
end
elseif tfclass
method = namesclass{locclass};
type = 'classification';
else
method = namesreg{locreg};
type = 'regression';
end
% Make sure the type is consistent
if ~isempty(usertype) && ~strcmp(usertype,type)
error(message('stats:classreg:learning:FitTemplate:make:UserTypeMismatch', method, usertype));
end
% Make template
temp = classreg.learning.FitTemplate(method,modelArgs);
temp = fillIfNeeded(temp,type);
end
You must change this function.

Related

Why won't my Decision Tree classifier work. The functions say not enough input arguments

I have coded a Decision Tree classifier in Matlab. To the best of my knowledge everything should work, the logic checks out. When I try to call the fit method it breaks on one of my functions telling me I haven't got the right input arguments but I'm sure I do! Been trying to solve this and similar errors to do with functions and input arguments for a day or two now. I wondered if it had something to do from calling them from within the constructor but calling them from the main script still doesn't work. Pls help!
classdef my_ClassificationTree < handle
properties
X % training examples
Y % training labels
MinParentSize % minimum parent node size
MaxNumSplits % maximum number of splits
Verbose % are we printing out debug as we go?
% MinLeafSize
CutPoint
CutPredictorIndex
Children
numSplits
root
end
methods
% constructor: implementing the fitting phase
function obj = my_ClassificationTree(X, Y, MinParentSize, MaxNumSplits, Verbose)
obj.X = X;
obj.Y = Y;
obj.MinParentSize = MinParentSize;
obj.MaxNumSplits = MaxNumSplits;
obj.Verbose = Verbose;
% obj.Children = zeros(1, 2);
% obj.CutPoint = 0;
% obj.CutPredictorIndex = 0;
% obj.MinLeafSize = MinLeafSize;
obj.numSplits = 0;
obj.root = Node(1, size(obj.X,1));
root = Node(1, size(obj.X,1));
fit(obj,root);
end
function node = Node(sIndex,eIndex)
node.startIndex = sIndex;
node.endIndex = eIndex;
node.leaf = false;
node.Children = 0;
node.size = eIndex - sIndex + 1;
node.CutPoint = 0;
node.CutPredictorIndex = 0;
node.NodeClass = 0;
end
function fit(obj,node)
if node.size < obj.MinParentSize || obj.numSplits >= obj.MaxNumSplits
% Mark the node as a leaf node
node.Leaf = true;
% Calculate the majority class label for the examples at this node
labels = obj.Y(node.startIndex:node.endIndex); %gather all the labels for the data in the nodes range
node.NodeClass = mode(labels); %find the most frequent label and classify the node as such
return;
end
bestCutPoint = findBestCutPoint(node, obj.X, obj.Y);
leftChild = Node(node.startIndex, bestCutPoint.CutIndex - 1);
rightChild = Node(bestSplit.splitIndex, node.endIndex);
obj.numSplits = obj.numSplits + 1;
node.CutPoint = bestSplit.CutPoint;
node.CutPredictorIndex = bestSplit.CutPredictorIndex;
%Attach the child nodes to the parent node
node.Children = [leftChild, rightChild];
% Recursively build the tree for the left and right child nodes
fit(obj, leftChild);
fit(obj, rightChild);
end
function bestCutPoint = findBestCutPoint(node, X, labels)
bestCutPoint.CutPoint = 0;
bestCutPoint.CutPredictorIndex = 0;
bestCutPoint.CutIndex = 0;
bestGDI = Inf; % Initialize the best GDI to a large value
% Loop through all the features
for i = 1:size(X, 2)
% Loop through all the unique values of the feature
values = unique(X(node.startIndex:node.endIndex, i));
for j = 1:length(values)
% Calculate the weighted impurity of the two resulting
% cut
leftLabels = labels(node.startIndex:node.endIndex, 1);
rightLabels = labels(node.startIndex:node.endIndex, 1);
leftLabels = leftLabels(X(node.startIndex:node.endIndex, i) < values(j));
rightLabels = rightLabels(X(node.startIndex:node.endIndex, i) >= values(j));
leftGDI = weightedGDI(leftLabels, labels);
rightGDI = weightedGDI(rightLabels, labels);
% Calculate the weighted impurity of the split
cutGDI = leftGDI + rightGDI;
% Update the best split if the current split has a lower GDI
if cutGDI < bestGDI
bestGDI = cutGDI;
bestCutPoint.CutPoint = values(j);
bestCutPoint.CutPredictorIndex = i;
bestCutPoint.CutIndex = find(X(:, i) == values(j), 1, 'first');
end
end
end
end
% the prediction phase:
function predictions = predict(obj, test_examples)
% get ready to store our predicted class labels:
predictions = categorical;
% Iterate over each example in X
for i = 1:size(test_examples, 1)
% Set the current node to be the root node
currentNode = obj.root;
% While the current node is not a leaf node
while ~currentNode.leaf
% Check the value of the predictor feature specified by the CutPredictorIndex property of the current node
value = test_examples(i, currentNode.CutPredictorIndex);
% If the value is less than the CutPoint of the current node, set the current node to be the left child of the current node
if value < currentNode.CutPoint
currentNode = currentNode.Children(1);
% If the value is greater than or equal to the CutPoint of the current node, set the current node to be the right child of the current node
else
currentNode = currentNode.Children(2);
end
end
% Once the current node is a leaf node, add the NodeClass of the current node to the predictions vector
predictions(i) = currentNode.NodeClass;
end
end
% add any other methods you want on the lines below...
end
end
This is the function that calls myClassificationTree
function m = my_fitctree(train_examples, train_labels, varargin)
% take an extra name-value pair allowing us to turn debug on:
p = inputParser;
addParameter(p, 'Verbose', false);
%addParameter(p, 'MinLeafSize', false);
% take an extra name-value pair allowing us to set the minimum
% parent size (10 by default):
addParameter(p, 'MinParentSize', 10);
% take an extra name-value pair allowing us to set the maximum
% number of splits (number of training examples-1 by default):
addParameter(p, 'MaxNumSplits', size(train_examples,1) - 1);
p.parse(varargin{:});
% use the supplied parameters to create a new my_ClassificationTree
% object:
m = my_ClassificationTree(train_examples, train_labels, ...
p.Results.MinParentSize, p.Results.MaxNumSplits, p.Results.Verbose);
end
that is my code from the main block of code
mym2_dt = my_fitctree(train_examples, train_labels, 'MinParentSize', 10)
These are the errors these are the errors
I'm expecting it to build a decision tree and fill it. However it breaks on the findBestCutPoint function and I cannot fix it
The first argument of class methods (except the constructor) should be an instance of the class (i.e obj). Your definition of Node and findBestCutPoint should have obj as the first argument.
Moreover, calls to class methods from within other methods should have the syntax obj.theMethod which seems not to be the case in your code.
So, for instance, the call to Node should be:
obj.root = obj.Node(1, size(obj.X,1));
and Node should be defined as follows:
function node = Node(obj,sIndex,eIndex)
Same applies to findBestCutPoint. Note that, in the calls, the reference to the class instance is passed implicitly, so you don't need to actually include it in the call.

Non scalar struct array

This is very complicated problem about data structures, I will try and explain it as simple as possible
I have multiple signals in a array signals, every element of this is a structure with multiple segments of the signal and its attribute.
Now I have another function which filters this signal and does some calculation according to the cut off frequency.
Whenever this function is called, I want to loop through all fc, and through all signals and through all segments. But the problem is fc is calculated only in a signal, so I have something like this:
classdef datHandle < handle
properties
error_norm = {};
final_error = {};
signals = {};
end
methods
function this = addsignal(this, varargin)
%signal segmentation is done here
end
function this = addfilter(this, varargin)
for i = 1:length(this.signals)% for each signal
this.error_norm = {};
fn = 1/((mean(diff(this.signals{i}(1).time)))*2);
fc = linspace(1,fn,(fn/0.5)); %calculate fc
this.tempaddfilt(fc,i)
end
this.final_error = [this.final_error;this.error_norm];
end
function this = tempaddfilt(this,varargin)
s = [];
f = ltiFilter.PT1(); % initiate filter class
fc = varargin{1}; % take fc
i = varargin{2}; % the exact signal
for a = 1:length(fc) % fc
q = 0;
w = 0;
for k = 1:length(this.segments{i}) % segment of ith signal
f.fc = fc(a);
filt_sig = f.eval(this.segments{i}(k).signal,this.segments{i}(k).signal(1)); %signal and the initial value of the signal
filt_sig = filt_sig';
s(1,i).main(k).seg_err(a) = std((filt_sig-this.segments{i}(k).ref)); % calculate the standard diviation of the signal
q = q+s(1,i).main(k).seg_err(a);
s(1,i).main(k).fc(a) = fc(a);
end
s(1,i).main(i).sig_err(a) = q;
w = w+s(1,i).main(i).sig_err(a);
end
s(1,1).main(1).filt_err(a) = w;
this.error_norm = [this.error_norm s];
end
end
end
test script:
clear all
close all
filname = load('file');
signal1 = filname.signal; % current value
time1 = filname.time;
signal2 = filname.signal2; % current value
time2 = filname.time2;
f = ltiFilter.datHandle();
f.addsignal(signal1,time1,93);
f.addfilter()
I planned the final_norm to be something like this:
But my algorithm doesn't work, when I add 2nd signal. If anyone has better algorithm any suggestion is welcome.
I don't fully understand, what is your data structure and why each signal is divided into segments? Do you have frame-based signal processing (chunk by chunk), or there is an algorithm for signal segmentation?
I think you should make more than one class with object vectorization along signals, filters and segments.

MATLAB: Converting from for loop, to no loop

I have a function provided by the instructor, which is not available for us to see the inner workings.
This function basically integrates whatever you input it.
Here is our loop:
for i = 1:length(time)-1
intdefValues(i) = intdef(t, r, time(i), time(i+1));
end
We are trying to do this:
intdefValues = intdef(t, r, time(1:end-1), time(2:end));
Are we wrong? Is there anyway that the function doesn't support vectors?
Syntax for the function in case you are curious;
intdef(t, x, a, b)
Where t = time vector, x = function, and a,b are the start / end parameters.
tic(); % ------------
% TEST ALSO
% PERFORMANCE:
% prepare another "vectorOfTimeENDs",
% aligned with your loop-ing strategy
% for a fully vectorised call
startsVector = time(1:end-1); % just a syntax-sugar, may omit for speed == time(1:end-1)
endsVector = time(2:end); % this one is important
intdefValues = intdef( tVector, rFunction, startsVector, endsVector );
toc()
% ---------------------------------------------------------------------------
tic(); % COMPARE WITH THE ORIGINAL FOR/LOOP:
for i = 1:length(time)-1
intdefValues(i) = intdef( tVector, rFunction, time(i), time(i+1) );
end
toc()

Extract a value from MATLAB codistributed array

I am attempting to build a distributed array of handle class objects in MATLAB and I would like to extract a specific handle from this vector for use on the command line. Given the code below, I would like the following to execute. I am running this on with two labs (matlabpool 2). The getValue function is what I need assistance with, thanks...
vec = buildArray;
h = getValue(vec,6);
h.id
ClassA.m:
A class that I would like to distribute in parallel.
classdef ClassA < handle
properties
id;
end
methods
function obj = ClassA(id)
obj.id = id;
end
end
end
buildArray.m:
A function to build codistributed array from local instances of ClassA.
function vec = buildArray
X(:,1) = 1:10; % create ids
gsize = size(X); % the global size
X = distributed(X); % distribute the ids
spmd
x = getLocalPart(X); % extract the local ids
local = cell(length(x),1); % create local storage for handles
% Create the class instances
for i = 1:length(x);
local{i} = ClassA(x(i));
end
% Build the codistributed array of handles
codist = codistributor1d(codistributor1d.unsetDimension, ...
codistributor1d.unsetPartition, gsize);
vec = codistributed.build(local,codist);
end
getValue.m:
This is the function that I need help with, currently it just displays what lab that contains the class with the specified id. I would like for it to return the handle so it may be used from the command line. How is this done?
function h = getValue(vec, id)
h = []; % just so it will not throw an error
spmd
local = getLocalPart(vec);
for i = 1:length(local);
if local{i}.id == id;
% Export h here
disp(['ID ', num2str(id), ' is on lab ', num2str(labindex)]);
break;
end
end
end
I was able to get my problem working using this for getValue.m, is there a better way?
function h = getValue(vec, id)
spmd
local = getLocalPart(vec);
idx = [];
for i = 1:length(local);
if local{i}.id == id;
idx = i;
break;
end
end
codist = codistributor1d(codistributor1d.unsetDimension, ...
codistributor1d.unsetPartition, [numlabs,1]);
IDX = codistributed.build(idx, codist);
end
c = gather(vec(IDX));
h = c{1};

Converting Matlab to Octave is there a containers.Map equivalent?

I am trying to convert some matlab code from the Maia package into something that will work with Octave. I am currently getting stuck because one of the files has several calls to containers.Map which is apparently something that has not yet been implemented in octave. Does anyone have any ideas for easily achieving similar functionality without doing a whole lot of extra work in octave? Thanks all for your time.
function [adj_direct contig_direct overlap names longest_path_direct...
weigth_direct deltafiles deltafiles_ref ReferenceAlignment ...
contig_ref overlap_ref name_hash_ref] = ...
assembly_driver(assemblies,ref_genome,target_chromosome, ...
deltafiles_ref,contig_ref, overlap_ref, ...
name_hash_ref, varargin)
% ASSEMBLY_DRIVER Combines contig sets into one assembled chromosome
%
% INPUT
% assemblies
% ref_chromosome
% Startnode_name
% Endnode_name
% OPTIONAL DEFAULT
% 'z_weigths' [.25 .25 .25 .25]
% 'clipping_thrs' 10
% 'ref_distance' -10
% 'ref_quality' 1E-5
% 'max_chromosome_dist' 100
% 'quit_treshold' 15
% 'tabu_time' 3
% 'minimum_improvement' -inf
% 'ref_node_assemblies' all assemblies (slow)
% 'endextend' true
%
%
% SET DEFAULTS
% General parameters
z_weights = [.25 .25 .25 .25];
clipping_thrs = 10;
mapfilter = '-rq';
alignlen = 75;
ident = 85;
% Reference nod parameters
ref_distance = -10;
ref_quality = 1E-5;
max_chromosome_dist = 100;
% TABU parameters
quit_treshold = 15;
tabu_time = 3;
minimum_improvement = -inf;
ref_node_assemblies = assemblies;
% Extending the assembly outwards from the start and en node
endextend = true;
AllowReverse = true;
% If no start and end node are given, they will be determined from tiling
Startnode_name = '';
Endnode_name = '';
containment_edge = true;
ref_first = true;
% If contigs have already been aligned to the reference, give the
% deltafile
ReferenceAlignment = 'NotYetDoneByMaia';
% Get VARARGIN user input
if length(varargin) > 0
while 1
switch varargin{1}
case 'Startnode_name'
Startnode_name = varargin{2};
case 'Endnode_name'
Endnode_name = varargin{2};
case 'z_weigths'
z_weights = varargin{2};
case 'clipping_thrs'
clipping_thrs = varargin{2};
case 'ref_distance'
ref_distance = varargin{2};
case 'ref_quality'
ref_quality = varargin{2};
case 'max_chromosome_dist'
max_chromosome_dist = varargin{2};
case 'quit_treshold'
quit_treshold = varargin{2};
case 'tabu_time'
tabu_time = varargin{2};
case 'minimum_improvement'
minimum_improvement = varargin{2};
case 'ref_node_assemblies'
ref_node_assemblies = assemblies(varargin{2},:);
case 'extend_ends'
endextend = assemblies(varargin{2},:);
case 'AllowReverse'
AllowReverse = varargin{2};
case 'ReferenceAlignment'
ReferenceAlignment = varargin{2};
case 'containment_edge'
containment_edge = varargin{2};
case 'ref_first'
ref_first = varargin{2};
case 'mapfilter'
mapfilter = varargin{2};
case 'alignlen'
alignlen = varargin{2};
case 'ident'
ident = varargin{2};
otherwise
error(['Input ' varargin{2} ' is not known']);
end
if length(varargin) > 2
varargin = varargin(3:end);
else
break;
end
end
end
% Read input assemblies
assembly_names = assemblies(:,1);
assembly_locs = assemblies(:,2);
assembly_quality = containers.Map(assemblies(:,1),assemblies(:,3));
assembly_quality('reference') = ref_quality;
% Read input assemblies for creation of reference nodes
ref_node_assembly_names = ref_node_assemblies(:,1);
ref_node_assembly_locs = ref_node_assemblies(:,2);
ref_node_assembly_quality = containers.Map(ref_node_assemblies(:,1),ref_node_assemblies(:,3));
ref_node_assembly_quality('reference') = ref_quality;
% If there is only one assembly there is nothing to align
if size(assemblies,1) >= 2
% Align assemblies against each other
assembly_pairs = {};
coordsfiles = [];
deltafiles = [];
for i = 1:length(assembly_locs)-1
for j = i+1:length(assembly_locs)
[coordsfile,deltafile] = align_assemblies({assembly_locs{i},assembly_locs{j}},{assembly_names{i}, assembly_names{j}}, ...
mapfilter, alignlen, ident);
coordsfiles = [coordsfiles; coordsfile];
%deltafiles = [deltafiles deltafile];
deltafiles = [deltafiles; {deltafile}];
assembly_pairs = [assembly_pairs;[assembly_names(i) assembly_names(j)]];
end
end
% fprintf('Loading alignment files.\n');
% load alignments_done;
% Put the nucmer alignments in an adjency matrix
%[adj, names, name_hash, contig, overlap] = get_adj_matrix(coordsfiles, assembly_pairs, assembly_quality, z_weights, 'clipping_thrs', clipping_thrs, 'dove_tail', 'double','edge_weight','z-scores', 'containment_edge', true);
[adj, names, name_hash, contig, overlap] = get_adj_matrix(deltafiles, assembly_pairs, assembly_quality, z_weights, 'clipping_thrs', clipping_thrs, 'dove_tail', 'double','edge_weight','z-scores', 'containment_edge', containment_edge);
% Merge deltafiles
deltafilesnew = deltafiles{1};
if size(deltafiles,1) > 1
for di = 2:size(deltafiles,1)
deltafilesnew = [deltafilesnew deltafiles{di}];
end
end
deltafiles = deltafilesnew;
else
assembly_pairs = {};
coordsfiles = [];
deltafiles = [];
adj = [];
names = {};
name_hash = containers.Map;
contig = struct('name',{},'size',[],'chromosome',[],'number',[], 'assembly', [], 'assembly_quality', []);
overlap = struct('Q',{},'R',[],'S1',[],'E1', [], 'S2', [], 'E2', [], 'LEN1', [], 'LEN2', [], 'IDY', [], 'COVR', [], 'COVQ', [],'LENR',[], 'LENQ',[]);
end
% Ad the pseudo nodes to the graph. If the contigs have already been
% aligned to the reference genome, just select the alignments that
% correspond to the target chromosome
if isequal(ReferenceAlignment,'NotYetDoneByMaia')
% Align all contigs in 'contig_sets_fasta' to the reference chromosome
[contig_ref, overlap_ref, name_hash_ref, deltafiles_ref] = align_contigs_sets(...
ref_genome, ref_node_assembly_locs, ref_node_assembly_names, ...
ref_node_assembly_quality, clipping_thrs, z_weights, ...
ref_distance,max_chromosome_dist);
ReferenceAlignment = 'out2.delta';
end
% Select only the entries in the deltafile for the current target chromosome
[contig_target_ref, overlap_target_ref, name_hash_target_ref, delta_target_ref] = ...
GetVariablesForTargetChromosome(...
contig_ref, overlap_ref, deltafiles_ref);
% Ref clipping should be high in case of tiling
%if isequal(max_chromosome_dist,'tiling')
% clipping_thrs = 10000
%end
% Add reference nodes to the adjency matrix
[adj, names, name_hash, contig, overlap, delta_target_ref, Startnode_name, Endnode_name] = get_reference_nodes( ...
adj, names, name_hash, contig, overlap, target_chromosome, ...
contig_target_ref, overlap_target_ref, name_hash_target_ref, delta_target_ref, ...
max_chromosome_dist, ref_distance, clipping_thrs, ref_first,...
Startnode_name, Endnode_name, AllowReverse);
% Give reference edges some small extra value to distict between
% assemblies to which a reference node leads
% adj = rank_reference_edges(adj,contig,assembly_quality);
% Specify a start and an end node for the assembly
Startnode = name_hash(Startnode_name);
Endnode = name_hash(Endnode_name);
% Find the best scoring path
fprintf('Directing the final graph\n');
% Calculate path on undirected graph to get an idea on how to direct the graph
[longest_path weigth] = longest_path_tabu(adj, Startnode, Endnode, quit_treshold, tabu_time, minimum_improvement);
% Make the graph directed (greedy)
[adj_direct contig_direct] = direct_graph(adj,overlap, contig, names, name_hash,clipping_thrs, Startnode, longest_path, true, ref_first);
% Calcultate final layout-path
fprintf('Find highest scoring path\n');
[longest_path_direct weigth_direct] = longest_path_tabu(adj_direct, Startnode, Endnode, quit_treshold, tabu_time, minimum_improvement);
function [contig_target_ref, overlap_target_ref, name_hash_target_ref, delta_target_ref] = ...
GetVariablesForTargetChromosome(...
contig_ref, overlap_ref, deltafiles_ref)
% Select only the entries in the deltafile for the current target chromosome
delta_target_ref = deltafiles_ref;
for di = size(delta_target_ref,2):-1:1
if ~isequal(delta_target_ref(di).R,target_chromosome)
delta_target_ref(di) = [];
end
end
overlap_target_ref = overlap_ref;
for oi = size(overlap_target_ref,2):-1:1
if ~isequal(overlap_target_ref(oi).R,target_chromosome)
overlap_target_ref(oi) = [];
end
end
contig_target_ref = contig_ref;
for ci = size(contig_target_ref,1):-1:1
if isequal(contig_target_ref(ci).assembly, 'reference') && ~isequal(contig_target_ref(ci).name,target_chromosome)
contig_target_ref(ci) = [];
end
end
name_hash_target_ref = make_hash({contig_target_ref.name}');
end
end
There is no exact equivalent of containers.Map in Octave that I know of...
One option is to use the java package to create java.util.Hashtable. Using this example:
pkg load java
d = javaObject("java.util.Hashtable");
d.put('a',1)
d.put('b',2)
d.put('c',3)
d.get('b')
If you are willing to do a bit of rewriting, you can use the builtin struct as a rudimentary hash table with strings (valid variable names) as keys, and pretty much anything stored in values.
For example, given the following:
keys = {'Mon','Tue','Wed'}
values = {10, 20, 30}
you could replace this:
map = containers.Map(keys,values);
map('Mon')
by:
s = struct();
for i=1:numel(keys)
s.(keys{i}) = values{i};
end
s.('Mon')
You might need to use genvarname to produce valid keys, or maybe a proper hashing function that produces valid key strings.
Also look into struct-related functions: getfield, setfield, isfield, fieldnames, rmfield, etc..