how to manipulate Recursive function parameters ?

4 visualizzazioni (ultimi 30 giorni)
Hello, i'm have some confusion in terms of recursive functions. Can someone explain to me why this recursive function calls itself without the same number of arguments initially defined ? is that possible? What's the goal of not taking the other inputs into cosideration ?
function [output1 output2 output3] = TimeCourse(input, mode, geneIndex, dataForCurrentGene, newLogHypers, newCovarianceMatrixInverses)
switch mode
case 'init'
data = input.data;
nGenes = input.nGenes;
nFeatures = input.nFeatures;
sparseMatrix = zeros(nGenes,nFeatures);
sparseVector = false(1,nGenes);
maxNumberOfComponents = input.maxNumberOfComponents;
featureNames = input.featureNames;
featureNames = cellfun(@str2num,featureNames);
[X, Y] = meshgrid(featureNames);
timeDiffs = (-(X - Y).^2);
hyperPriorParameters = [0, 1; 0, 1; 0, 1]; % [mean s.d.; ...]
lowerTriangularLogicalMatrix = logical(tril(ones(nFeatures)));
% Define the cluster structure
clusterStruct(1,(maxNumberOfComponents+1)) = struct(...
'nFeatures', [], ...
'nGenesOverall', [], ...
'timeDiffs', [],...
'logHypers', [], ...
'logPriorOfLogHypers', [], ...
'squaredHypers', [], ...
'hyperPriorParams', [], ...
'lowerTriangularPartOfCovarianceMatrix', [], ...
'covarianceMatrixInverses', [], ...
'nGenes', [], ...
'logMarginalLikelihood', [],...
'dataCounts', [], ...
'squaredDataCounts', [], ...
'logicalGeneIDs', [], ...
'lowerTriangularLogicalMatrix', [], ...
'N', []);
[clusterStruct.nFeatures ] = deal(nFeatures);
[clusterStruct.nGenesOverall ] = deal(nGenes);
[clusterStruct.hyperPriorParams] = deal(hyperPriorParameters);
[clusterStruct.timeDiffs] = deal(timeDiffs);
[clusterStruct.lowerTriangularLogicalMatrix] = deal(lowerTriangularLogicalMatrix);
[clusterStruct.logMarginalLikelihood] = deal(0);
[clusterStruct.nGenes] = deal(0);
[clusterStruct.logicalGeneIDs] = deal(sparseVector);
% Initialise clusters:
nStartingClusters = ceil(log(nGenes));
clusterIDs = random('unid', nStartingClusters, 1, nGenes); %row vector
uniqueIDs = unique(clusterIDs);
for i = 1:maxNumberOfComponents
clusterStruct(i).covarianceMatrixInverses(1,nGenes) =...
struct('invertedCovarianceMatrix', [], 'determinant', []);
end
for i = uniqueIDs
logicalIndices = clusterIDs == i;
indices = find(logicalIndices);
nGenesInCluster = length(indices);
dataInCluster = sparseMatrix;
dataInCluster(indices,:) = data(logicalIndices,:);
currentCluster = clusterStruct(i);
currentCluster.logicalGeneIDs = logicalIndices;
currentCluster.dataCounts = sum(dataInCluster,1);
currentCluster.squaredDataCounts = sum(dataInCluster.^2,1);
currentCluster.nGenes = nGenesInCluster;
currentCluster.N = nFeatures*nGenesInCluster;
logHypers = TimeCourse(currentCluster, 'sampleHypers');

Risposta accettata

Thiago Henrique Gomes Lobato
You didn't post the whole function, but regardless of this: It is possible to call a function without passing all the parameters as long as you handle this in the code itself. There are many reasons to do this, but all go around using a somewhat different function depending of the parameters without having to create many different functions with very similar code.
  2 Commenti
wejden hammami
wejden hammami il 31 Mag 2020
is that possible also in python ? (calling the function without all input arguments ) ?
Thiago Henrique Gomes Lobato
Yes and no. If you have a function defined, for example, as:
def Test(A,B):
return A+1
Test(1)
You will have an error even if you not give B even though it is useless. An alternative is to use standard values:
def Test(A=0,B=0):
return A+1
Test(1)
This will work, so you would have standard values for all your inputs and could pass only the ones you want to. This, I beleive, would be the best way for your function. Another more complicate solution is to use multipledispatch, but then you will have to define all functions variations.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Parallel Computing in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by