【问题标题】:Calling a function in matlabmatlab中调用函数
【发布时间】:2012-10-29 21:08:28
【问题描述】:

我在 matlab 中有一个 MinCUT 算法。这是一个功能!我如何从我的邮件文件中调用它! 我必须定义所有变量吗?还是我必须只定义输入?

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

【问题讨论】:

    标签: algorithm function matlab partition-problem


    【解决方案1】:

    将函数定义放在一个名为 MinCut.m 的文件中,该文件必须位于您当前的 matlab 路径(例如您当前的工作目录)中。

    然后就可以调用函数编写了

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

    也可以考虑this answer (Matlab: Calling user defined function)

    【讨论】:

      【解决方案2】:

      你可以这样称呼它:

      MinCut(SourceNodes, WeightedGraph)
      

      或者如果你想收集输出

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

      【讨论】:

        猜你喜欢
        • 2015-04-08
        • 1970-01-01
        • 2021-12-13
        • 2017-05-15
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多