how to manipulate Recursive function parameters ?
3 次查看(过去 30 天)
显示 更早的评论
wejden hammami
2020-5-31
评论: Thiago Henrique Gomes Lobato
,2020-5-31
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');
0 个评论
采纳的回答
Thiago Henrique Gomes Lobato
2020-5-31
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 个评论
Thiago Henrique Gomes Lobato
2020-5-31
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!