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

  • SEARCH
  • Home
  • 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 8806253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:05:08+00:00 2026-06-14T02:05:08+00:00

I have a question about how can I construct a loop. I have a

  • 0

I have a question about how can I construct a loop. I have a double array and I want to code that process.

This is the array where I want to apply the process and the length is 4×4 (I’m doing only the first, but the original was 4x4x3).

b1= (:,:,1);

I want to apply for every value this process:

1.- Create a vector every time, with the information of every value of the array.

ma= 0;
 for p=(136:136)
 ma(p)=b1(1,1,1);
 end
 for p=(312:2151)
     ma(p)=0
 end
 ma= ma';

Then, I have to do with the last result the following process (I’ve defined the variables before).
spout1_a= spb1y.ma;
spout1_b= spout1_a./spsum_pesos1;
spout1_c= sum(spout1_b);

The problem is I know how can to do it for one value (the first one), but not with all of them. How can I do it?

Greetings and thank you so much,

Emma

EDIT

b1= Refl(:,:,1);

load sp1.txt;

spb1y= sp1(:,1);
spsum_pesos1= sum(spb1y);
output = cell(length(banda1), 5); % this works well

 for i = 1:numel(b1)
    ma = zeros(2151,1);
    ma(136) = output(i);
    spout1_a= spb1y.*ma;
    spout1_b= spout1_a./spsum_pesos1;
    spout1_c= sum(spout1_b); % I want to save that result on every value of the matrix
end 

Am I writing this last part well?

  • 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-06-14T02:05:09+00:00Added an answer on June 14, 2026 at 2:05 am

    Here are a few pointers for coding in MATLAB:

    1. Preallocate: allocate memory prior to the loop so that variables don’t grow in dimensions inside the loop. This causes MATLAB to dynamically allocate memory in each iterations, which can often slow execution time significantly.
    2. Vecotrize: try to use loops only where you have to. In many cases, you can use vectorized operations instead, which are a lot faster.

    With that said, you can try this:

    % # Iterate over each value in 'b1'
    for i = 1:numel(b1)
    
        % # Create a vector 'ma'
        ma = zeros(2151, 1);
        ma(136) = b1(i);
    
        % # Do some more calculations with 'ma' ...
        spout1_a = ma;
        spout1_b= spout1_a ./ spsum_pesos1;
        spout1_c= sum(spout1_b);
    end
    

    I’m not sure what you want to achieve, but this code does exactly what the question describes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple question about MVC routing. How i can construct Html.ActionLink thhat generates
I have the following question about JPA: Can I save the order of the
I have a question about JSF and GET http request. How can I get
I have a question about a problem I'm struggling with. Hope you can bear
I have a question about joptionpane. Using JOptionPane.showMessageDialog(...), we can create a message dialog.
So, i have some question about xml Documents in Java. Can i get all
Can anyone help me with a question about webservices and scalability? I have written
How would you write this code? This particular question is about a maze game
I'm developing an iPhone app that can edit images. Ultimately, I want to have
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (

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.