简体   繁体   English

在Matlab中调用函数

[英]Calling a function in matlab

I have a MinCUT Algorithm In matlab. 我在Matlab中有一个MinCUT算法。 It is a function! 是功能! How do I call it from my mail file! 如何从我的邮件文件中调用它! Do I have to define all the variables? 我必须定义所有变量吗? Or do I have to define just the Inputs? 还是只需要定义输入?

function [MinCutGroupsList, MinCutWeight] = MinCut(SourceNodes, WeightedGraph)
%%% performs Min Cut algorithm described in "A Simple Min Cut Algorithm" by
%%% M. Stoer and F. Wagner.

%%% input - 
%%%     SourceNodes - a list of Nodes that are forced to be kept in one side of the cut.
%%%     WeightedGraph - symetric matrix of edge weights. Wi,j is the edge connecting Nodes i,j
%%%                     use Wi,j=0 or Wi,j == inf to indicate unconnected Nodes.
%%% output -
%%%   MinCutGroupsList - two lists of verices, SECOND one contains the sourve vertives
%%%   MinCutWeight - sum of weight of edges alosng the cut

    GraphDim = size(WeightedGraph,1);
    SourceNodes = SourceNodes(SourceNodes ~= 0); %remove zero Nodes

    %%% remove self edges and ZEROed ones
    WeightedGraph = WeightedGraph+diag(inf(1,GraphDim));
    % for ii = 1:GraphDim
    %     WeightedGraph(ii,ii) = inf;
    % end
    WeightedGraph(WeightedGraph == 0) = inf;

    %%%Merge all Source Vrtices to one, so they'll be unbreakable, descending order is VITAL!!!
    SourceNodes = sort(SourceNodes);
    GroupsList = zeros(GraphDim);   %each row are the Nodes melted into one vertex in the table.
    GroupsList(:,1) = 1:GraphDim;
    for ii=length(SourceNodes):-1:2;
        [WeightedGraph,GroupsList] = MeltTwoNodes(SourceNodes(1),SourceNodes(ii),WeightedGraph,GroupsList);
    end
    Split = GroupsList(:,1);

    %%% By now I have a weighted graph in which all seed Nodes are
    %%% merged into one vertex. Run Mincut algrithm on this graph
    [MinCutGroupsList_L, MinCutWeight] = MinCutNoSeed(WeightedGraph);

    %%% Convert Data so the seed Nodes will be reconsidered as different
    %%% Nodes and not one vertex.
    for ii = 1:2
        MinCutGroupsList(ii,:) = Local2GlobalIndices(MinCutGroupsList_L(ii,:), Split);
    end

    if (length(find(MinCutGroupsList(1,:) == SourceNodes(1))) == 1)
        SeedLocation = 1;
    else
        SeedLocation = 2;
    end
    MinCutGroupsList_withSeed = [MinCutGroupsList(SeedLocation,(MinCutGroupsList(SeedLocation,:)~=0)) SourceNodes(2:length(SourceNodes))];
    MinCutGroupsList_withSeed = sort(MinCutGroupsList_withSeed);
    MinCutGroupsList_withSeed = [MinCutGroupsList_withSeed zeros(1,GraphDim - length(MinCutGroupsList_withSeed))];

    MinCutGroupsList_NoSeed = MinCutGroupsList(3 - SeedLocation,(MinCutGroupsList(3 - SeedLocation,:)~=0));
    MinCutGroupsList_NoSeed = sort(MinCutGroupsList_NoSeed);
    MinCutGroupsList_NoSeed = [MinCutGroupsList_NoSeed zeros(1,GraphDim - length(MinCutGroupsList_NoSeed))];

    MinCutGroupsList = [MinCutGroupsList_NoSeed ; MinCutGroupsList_withSeed];

return

Put the function definition in a file named MinCut.m which must be in your current matlab path (eg your current working directory). 将函数定义放在名为MinCut.m的文件中,该文件必须位于当前的matlab路径中(例如,当前的工作目录)。

Then you will be able to call the function writing 然后您将可以调用该函数编写

 [MinCutGroupsList, MinCutWeight] = MinCut(yourSourceNodes, yourWeightedGraph); 

consider also this answer (Matlab: Calling user defined function) . 也考虑这个答案(Matlab:调用用户定义的函数)

You can call it like this: 您可以这样称呼它:

MinCut(SourceNodes, WeightedGraph)

or if you want the output to be collected 或者如果您想收集输出

[MinCutGroupsList, MinCutWeight] = MinCut(SourceNodes, WeightedGraph)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM