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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:06:21+00:00 2026-05-23T00:06:21+00:00

Is there a java version of matlab’s colon operator or linspace? For instance, I’d

  • 0

Is there a java version of matlab’s colon operator or linspace? For instance, I’d like to make a for loop for evenly spaced numbers, but I don’t want to bother with creating an array of those numbers manually.

For example to get all integers from 1 to 30, in matlab I would type:

1:30

or

linspace(1,30)
  • 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-23T00:06:22+00:00Added an answer on May 23, 2026 at 12:06 am

    For the two variable call, @x4u is correct. The three variable call will be quite a bit harder to emulate.

    For instance, i think that linspace(1,30,60) should produce values 1, 1.5, 2, 2.5, 3, 3.5…, or maybe that’s the values for linspace(1,30,59)–either way, same problem.

    With this format you’ll have to do the calculations yourself–Personally I’d create a new object to do the whole thing for me and forget the for loop.

    counter=new Linspace(1,30,60);
    while(counter.hasNext()) {
        process(counter.getNextFloat())
    }
    

    or simply

    while(float f : new Linspace(1,30,60)) {
        process(f);
    }
    

    if you have your Linspace object implement Iterable.

    Then the inside of the counter object should be pretty obvious to implement and it will easily communicate to you what it is doing without obfuscating your code with a bunch of numeric calculations to figure out ratios.

    An implementation might be something like this:
    (NOTE: Untested and I’m pretty sure this would be vulnerable to edge cases and floating point errors! It also probably won’t handle end < start for backwards counting, it’s just a suggestion to get you going.)

    public class Linspace {
        private float current;
        private final float end;
        private final float step;
        public Linspace(float start, float end, float totalCount) {
            this.current=start;
            this.end=end;
            this.step=(end - start) / totalCount;
        }
        public boolean hasNext() {
            return current < (end + step/2); //MAY stop floating point error
        }
        public float getNextFloat() {
            current+=step;
            return current;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there Java or .NET version of R? (like Jython / IronPython for the
Java is nearing version 7. It occurs to me that there must be plenty
Is there a reliable way of detecting what version of Java is installed on
Is there a Java package providing funcionality like the .Net System.Data namespace ? Specificaly
When using java from Matlab, is there some way to figure out from where
is there a Java version of the PHP function checkdnsrr ?
I would like to know which version of java is installed on my ubuntu
Is there a OS command to find what Java version Tomcat6 is using? I
Is there a way to get the version of MATLAB without launching the engine?
Is there a Java equivalent to .NET's App.Config? If not is there a standard

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.