如何在Matlab中进行矢量化?

发布于 2024-11-19 11:24:43 字数 1315 浏览 2 评论 0 原文

我想向量化这两行代码。我最近刚刚了解矢量化。我知道如何矢量化 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 

I would like to vectorize these two lines of code. I just recently learned about vectorization. I know how to vectorize the sumsurface line but I am not sure how to include the if statement, I would really like to vectorize the whole for loop and get rid of it. I want to vectorize to improve runtime the code I have right now runs very slow. I preallocated the arrays which helps improve runtime. I had forgotten to do that previously. If I could get any help that would be much appreciated.

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-11-26 11:24:43

您可以对整个算法进行矢量化。我不会为您全部编写代码,但这里有一些指导您入门:

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

You can vectorize the whole algorithm. I'm not going to code it all out for you but here are some pointers to get you started:

  • Use REPMAT to create an array that contains as many copies of pH as there are iterations, i.e. len.
  • Change all those variables beginning with n from scalars to vectors. For example, nAsp = randi([10, 30], [len, 1])
  • Use FIND to determine the indices of sumsurface that match your criteria, i.e. index = find(sumsurface < 0.01 & sumsurface > -0.01);.
  • Create your desired vectors using index, e.g. aspl = nAsp(index);
  • Rinse. Repeat.
离去的眼神 2024-11-26 11:24:43

以下是一种可能的向量化:

%# 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 将包含每次迭代的 pH 和 < code>params 分别满足您的方程:-0.01(如果存在)。

Here is one possible vectorization:

%# 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

After running the code, the cell-arrays x and p will contain, for each iteration, the pH and params respectively that satisfy your equation: -0.01<sumsurface<0.01 (if they exist).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文