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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:43:09+00:00 2026-05-26T09:43:09+00:00

In MATLAB the following for loop: for i = [1:100]’ %’// Do something, such

  • 0

In MATLAB the following for loop:

for i = [1:100]' 
    %'// Do something, such as disp(i)
end

isn’t apparently really implemented by iteration, rather i becomes the matrix [1 2 3 … 100] and the “loop” is only executed once on this matrix i. You can verify this by printing the value of i or other tracking information. Only a single pass is made through the loop.

Is it possible to force MATLAB to do genuine looping? The reason I ask is that the above approach is fine for many cases but much more painful when you have nested loops that need to run.

Example:
The following code won’t do what you would expect if you thought you were getting actual iteration over a loop:

for i = outlier
    data(i) = median(data(i-100:i+100))
end

One would expect at each outlier index this would replace data(i) with the median of the data from i-100 to i+100, but it doesn’t. In fact, the median returns a single value computed on a conglomerate of all the ranges you cared about, and every data(i) point is replaced with that single value.

  • 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-26T09:43:10+00:00Added an answer on May 26, 2026 at 9:43 am

    If you write

    for i = (1:100)' %'# square brackets would work as well
       doSomething
    end
    

    the loop is executed only once, since a for-loop iterates over all columns of whatever is to the right of the equal sign (it would iterate 200 times with a 100-by-200 array to the right of the equal sign).

    However, in your example, you have i=[1:100], which evaluates to a row vector. Thus, the loop should execute 100x.

    If you iterate over an array that might be nx1 instead of 1xn, you can, for safety reasons, write:

    for i = myArray(:)'  %'# guarantee nx1, then transpose to 1xn
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following loop reads data from excel into matlab with the use of actxserver.
The following MATLAB code does not work. I guess it has something to do
I normalize a vector V in MATLAB as following: normalized_V = V/norm(V); however, is
I have the following matlab code: randarray = gpuArray(rand(N,1)); N = 1000; tic g=0;
I want to vectorize the following MATLAB code. I think it must be simple
The x-axis and y-axis are interchanged in the figure by running the following matlab
Say I have a string in a variable in MATLAB like the following: this
The following stackoverflow qestion: Matlab: How to obtain all the axes handles in a
I want to display all the files in a folder in matlab. The following
In the following Matlab code, with nodes N=10 , you will get randomly picked

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.