【问题标题】:How to run two functions at the same time in matlab and wait till any one is completed?如何在matlab中同时运行两个函数并等待任何一个完成?
【发布时间】:2017-06-01 07:20:52
【问题描述】:

我有两个功能,都需要同时运行。然而,它只是速度测试,所以每当他们中的任何一个抛出输出并完成他们的部分执行时,'.m' 的执行都应该停止。

【问题讨论】:

  • 这是一个速度测试?您知道您可以对函数进行计时,以便确切知道它们需要多少时间?

标签: multithreading matlab function output execution


【解决方案1】:

使用 parfor 运行两个函数怎么样?检查以下代码:

funcs = {@exp,@sin} ;
args = {2,pi/4} ;
sols = cell(1,2) ;
parfor n = 1:2
    sols{n}=funcs{n}(args{n});
end
M = sols{1} ; N = sols{2} ;

【讨论】:

    【解决方案2】:

    也许是这样的?

    p = gcp();
    funcs = {@exp,@sin};
    for i=1:numel(funcs)
        f(i) = parfeval(p,funcs{i},1,10);
    end
    
    while true
      [idx,value] = fetchNext(f);
      fprintf('Fn indx: %d, Result: %d', idx, value)
      break;
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多