English 中文(简体)
• 如何简化Sharma和Matlab的 lo
原标题:How to simplify for loops in Python and Matlab

I m a C and MATLABuser. 当我开始学习时(一周前)我注意到,我没有充分利用MATLAB的全部潜力,特别是阵列行动。 我经常使用假体,可能是因为我学习了C的节目。

在上一个图普,我学会使用<代码>cumsum和其他高效阵列操作,例如:

alpha = [1e-4,1e-3,1e-4,1e-1,1e-2,1e-3,1e-6,1e-3];
zeta = alpha / (dz*dz)
nz = 101
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.2];
wz = cumsum(l*(nz-1));
nl = lenght(l);   

是否有可能简化以下法规:

      A = zeros(nz,nz);
      i=1;
      for j = 2:wz(i)-1
        A(j,j-1) = zeta(1,1);
        A(j,j) = -2*zeta(1,1);
        A(j,j+1) = zeta(1,1); % layer 1 nodes 
      end

      %cicle to n-layers
      for i=2:nl
          for j=wz(i-1):wz(i-1)
              A(j,j-1) = zeta(1,i-1);
              A(j,j) = -zeta(1,i-1)-zeta(1,i);
              A(j,j+1) = zeta(1,i); 
          end

          for j=wz(i-1)+1:wz(i)
              A(j,j-1) = zeta(1,i);
              A(j,j) = -2*zeta(1,i);
              A(j,j+1) = zeta(1,i);
          end

      end
end
最佳回答

我在有机会与你一道在我机器旁边管理该守则之后,修改了该守则。 仍然存在几个问题(在最后的路口里,是否大得多? 在此之前,你面临的问题是,我原谅的id子必须合乎逻辑。

dz=0.1;
alpha = [1e-4,1e-3,1e-4,1e-1,1e-2,1e-3,1e-6,1e-3];
zeta = alpha / (dz*dz);
nz = 101;
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.2];
wz = cumsum(l*(nz-1));
nl = length(l);

A = zeros(nz);
i=1;

%replaces 1st loop
j_start = 2;
j_end = wz(i)-1;

idx_matrix = false(size(A));
idx_matrix(j_start:j_end,j_start:j_end) = eye(j_end-j_start+1);
A(idx_matrix) = -2*zeta(1,1);

idx_matrix(idx_matrix) = false;
idx_matrix(j_start:j_end,j_start-1:j_end-1) = eye(j_end-j_start+1);
A(idx_matrix) = zeta(1,1);

idx_matrix(idx_matrix) = false;
idx_matrix(j_start:j_end,j_start+1:j_end+1) = eye(j_end-j_start+1);
A(idx_matrix) = zeta(1,1);

%cicle to n-layers
for i=2:nl

    %replaces 3rd loop
    j_start = wz(i-1);
    A(j_start,j_start) = -zeta(1,i-1)-zeta(1,i);
    A(j_start,j_start-1) = zeta(1,i-1);
    A(j_start,j_start+1) = zeta(1,i);

    %replaces 4th loop
    j_start = wz(i-1)+1;
    j_end = min(wz(i),size(A,2)-1);
    idx_matrix = false(size(A));
    idx_matrix(j_start:j_end,j_start:j_end) = eye(j_end-j_start+1);
    A(idx_matrix) = -2*zeta(1,i);

    idx_matrix(idx_matrix) = false;
    idx_matrix(j_start:j_end,j_start-1:j_end-1) = eye(j_end-j_start+1);
    A(idx_matrix) = zeta(1,i);

    idx_matrix(idx_matrix) = false;
    idx_matrix(j_start:j_end,j_start+1:j_end+1) = eye(j_end-j_start+1);
    A(idx_matrix) = zeta(1,i);

end
问题回答

为简化您的行程,您可使用<>条码>。

rel=“nofollow” http://www.mathwork.fr/help/techdoc/ref/spdiags.html

例如,你的第一例假可以写成:

A=full(spdiags(repmat([zeta(1,1),-2*zeta(1,1),zeta(1,1)],wz(i),1),[-1 0 1],wz(i),wz(i)))




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签