Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:34:26+00:00 2026-05-28T15:34:26+00:00

I’m a C and MATLAB user. When I started learning Python (a week ago)

  • 0

I’m a C and MATLAB user. When I started learning Python (a week ago) I noticed that I don’t use full potential of MATLAB, in particular array operations. I use for loops often, probably because I learnt programming in C.

In a previous tip, I learnt to use cumsum and other efficient array operations, for example:

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);   

Is it possible simplify the following code in Python (Numpy) or MATLAB?

      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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:34:27+00:00Added an answer on May 28, 2026 at 3:34 pm

    I’ve modified the code below after having a chance to run it on my machine side by side yours. There are still a couple of questions (is A suppose to get larger in the final loop?, what is dz?). The problem you ran into before running this was that I forgot idx_matrix had to be logical.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.