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 7026001
In Process

The Archive Base Latest Questions

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

I have a simple Matlab program that uses a set of random number lists

  • 0

I have a simple Matlab program that uses a set of random number lists and runs a series of trials using those numbers. Right now, the trials are run iteratively using the code below. How could this code be modified to eliminate the need for that iterative step? The program would be a lot more efficient if it could be properly vectorized.

size = 1000;
trials = 1000;

grid = zeros(size,size);


rx1 = randi(size,trials,1);
ry1 = randi(size,trials,1);
rx2 = randi(size,trials,1);
ry2 = randi(size,trials,1);

xmin = min(rx1,rx2);
xmax = max(rx1,rx2);
ymin = min(ry1,ry2);
ymax = max(ry1,ry2);

%This is the loop that I want to eliminate
for n=1:trials;
    grid(ymin(n):ymax(n),xmin(n):xmax(n)) = grid(ymin(n):ymax(n),xmin(n):xmax(n)) + 1;
end

figure
mesh(grid);
  • 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-28T00:03:35+00:00Added an answer on May 28, 2026 at 12:03 am

    I would use a trick inspired by integral images:

    grid(ymin(n):ymax(n),xmin(n):xmax(n))=1;
    

    is equivalent to:

    grid(ymin(n),xmin(n))=1;
    grid(ymin(n),xmax(n)+1)=-1;
    grid(ymax(n)+1,xmin(n))=-1;
    grid(ymax(n)+1,xmax(n)+1)=1;
    grid=cumsum(cumsum(grid,1),2);
    

    So for your problem I would do:

    grid = zeros(size+1,size+1);
    grid=full( sparse(ymin,xmin,1,size+1,size+1)...
          +sparse(ymax+1,xmax+1,1,size+1,size+1)...
          -sparse(ymin,xmax+1,1,size+1,size+1)...
          -sparse(ymax+1,xmin,1,size+1,size+1));
    grid=cumsum(cumsum(grid,1),2);
    grid=grid(1:end-1,1:end-1);
    

    I’ve tested it on my laptop. Results are same:

    Elapsed time for code with loop is 1.802788 seconds.

    Elapsed time for vectorized code is 0.033834 seconds.

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

Sidebar

Related Questions

I have some differential equations that I need to solve using MATLAB's ODE solvers.
I have written a simple brainfuck interpreter in MATLAB script language. It is fed
I have simple win service, that executes few tasks periodically. How should I pass
I am trying to achieve a very simple thing in MATLAB. I have a
A simple question, but I'm not so great with MATLAB. I have vectors x
I am new to MATLAB and I want to write a simple program to
I've written a very simple GUI in MATLAB that will convert temperatures. It is
I'm pretty new to MATLAB and I have a simple question. What if I
Hey guys, I have a very simple problem in MATLAB: I have some strings
I have some MATLAB code and some Java code that need to talk with

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.