【问题标题】:avoid eval for the sake of eval Matlab为了评估而避免评估 Matlab
【发布时间】:2014-06-13 03:58:09
【问题描述】:

我目前正在将我的数组评估为eval(['P' num2str(jj) '(i,:)']),其中P1P2 是我的数组,我只对(i,1:3) 和i 用于的所有数组感兴趣rows 和 jj 用于评估 1 2 数组。我想避免使用eval(),因为我想使用 PCT 来对两个数组进行并行处理,因为parfor 循环不支持eval() 和Matlab 还建议尽可能避免使用它,现在根据我的情况有哪些可能的解决方案/建议。

我的代码:

n=2;
for i=1:10
for jj=1:n
eval(['P' num2str(jj) '(i,:)']);
end
end

【问题讨论】:

    标签: arrays matlab for-loop matrix parfor


    【解决方案1】:

    尝试使用元胞数组:

    P = cell(1,2);
    P{1} = P1;
    P{2} = P2;
    

    现在你可以这样做了:

    n=2;
    for i=1:10
        for jj=1:n
            P{jj}(i,:)
        end
    end
    

    【讨论】:

    • 看起来很酷,但其他一些方法会更好,因为我也想避免 P1 和 P2。如果您有任何想法,请您提出其他选择吗?
    • 好吧,也许你可以做那个作业,然后运行clear P1 P2 将它们从内存中清除,从现在开始,只使用P{}。
    猜你喜欢
    • 2010-10-03
    • 2011-04-03
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多