【问题标题】:How to vectorize in Matlab?Matlab中如何向量化?
【发布时间】:2011-07-14 23:40:49
【问题描述】:

我想对这两行代码进行矢量化处理。我最近才了解矢量化。我知道如何矢量化 sumsurface 线,但我不确定如何包含 if 语句,我真的很想矢量化整个 for 循环并摆脱它。我想矢量化以改善运行时我现在拥有的代码运行速度非常慢。我预先分配了有助于提高运行时间的数组。我以前忘记这样做了。如果我能得到任何帮助,将不胜感激。

pH = linspace(2,12, 6000);
for j = 1:300
    nAsp = randi([10, 30],[1,1]);%865
    nGlu = randi([12, 38],[1,1]);%1074
    nLys = randi([11, 33],[1,1]);%930
    nArg = randi([10, 30],[1,1]);%879
    nCys = randi([2, 8],[1,1]); %214
    nTyr = randi([5, 17],[1,1]); %462
    nHis = randi([4, 12],[1,1]); %360   

    for i = 1: len;

        sumsurface(i) = (nAsp).*(-(10.^((pH(i)-asp) )./(10.^((pH(i)-asp) )+1)) )+ (nGlu).*(-(10.^((pH(i)-glu) )./(10.^((pH(i)-glu) )+1)))+(nCys).*(-(10.^((pH(i)-cys) )./(10.^((pH(i)-cys) )+1)))+ (nTyr).* (-(10.^((pH(i)-tyr) )./(10.^((pH(i)-tyr) )+1)))+ (nHis).*(1./(10.^((pH(i)-his) )+1))+ (nLys).*(1./(10.^((pH(i)-lys) )+1))+ (nArg).*(1/(10.^((pH(i)-arg) )+1));
        if sumsurface(i) < .01 && sumsurface(i) > -.01
            %disp(sumsurface(i));
            disp(pH(i));
            x(1+end) = pH(i);
            aspl(1+end) = nAsp;
            glul(1+end) = nGlu;
            cysl(1+end) = nCys;
            tyrl(1+end) = nTyr;
            hisl(1+end) = nHis;
            lysl(1+end) = nLys;
            argl(1+end) = nArg;                 

        end
    end    
 end 

【问题讨论】:

    标签: matlab vectorization


    【解决方案1】:

    您可以向量化整个算法。我不会为您编写所有代码,但这里有一些提示可以帮助您入门:

    • 使用REPMAT 创建一个数组,其中包含与迭代次数一样多的pH 副本,即len
    • 将所有以n 开头的变量从标量更改为向量。例如,nAsp = randi([10, 30], [len, 1])
    • 使用FIND 确定sumsurface 的索引符合您的条件,即index = find(sumsurface &lt; 0.01 &amp; sumsurface &gt; -0.01);
    • 使用index 创建您想要的向量,例如aspl = nAsp(index);
    • 冲洗。重复。

    【讨论】:

    • 我更新了我的代码,我意识到我遗漏了一个重要部分。我有一个双循环,但向量的长度不同。我不希望 pH 向量与 nAsp = randi([10, 30], [len, 1]) 向量的长度相同。是否仍然可以将其矢量化?谢谢指点
    【解决方案2】:

    这是一种可能的矢量化:

    %# data
    len = 6000;
    pH = linspace(2,12, len);
    
    %# some constants (fill your values here)
    asp = 0; glu = 0; cys = 0; tyr = 0; his = 0; lys = 0; arg = 0;
    
    %# random parameters for each iteration
    num = 300;
    nAsp = randi([10 30], [num 1]);
    nGlu = randi([12 38], [num 1]);
    nLys = randi([11 33], [num 1]);
    nArg = randi([10 30], [num 1]);
    nCys = randi([2 8]  , [num 1]);
    nTyr = randi([5 17] , [num 1]);
    nHis = randi([4 12] , [num 1]);
    
    params = [nAsp nGlu nCys nTyr nHis nLys nArg];
    M = [
        - 10.^(pH-asp) ./ (1 + 10.^(pH-asp))
        - 10.^(pH-glu) ./ (1 + 10.^(pH-glu))
        - 10.^(pH-cys) ./ (1 + 10.^(pH-cys))
        - 10.^(pH-tyr) ./ (1 + 10.^(pH-tyr))
        1 ./ (1 + 10.^(pH-his))
        1 ./ (1 + 10.^(pH-lys))
        1 ./ (1 + 10.^(pH-arg))
    ];
    
    %# iterations
    sumsurface = zeros(num,len);
    x = cell(num,1); p = cell(num,1);
    for j=1:num
        sumsurface(j,:) = params(j,:)*M;
    
        idx =  abs(sumsurface(j,:)) < 0.01;
        if any(idx)
            x{j} = pH(idx);
            p{j} = params(j,:);    %# [aspl glul cysl tyrl hisl lysl argl]
        end
    end
    

    运行代码后,单元格数组xp 将分别包含满足您的方程的pHparams-0.01&lt;sumsurface&lt;0.01(如果存在)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 2015-07-06
      • 2013-02-18
      相关资源
      最近更新 更多