Java version of Matlab Linspace

Is there a version of the java version of the operator or Linspace for working with metalab? For example, I would like to create a for loop for evenly distributed numbers, but I don't want to worry about creating an array of these numbers manually.

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

1:30

or

linspace(1,30)
+3
source share
6 answers

For two variable calls, @ x4u is correct. The third call to the variable will be much more difficult to imitate.

For example, I think linspace (1,30,60) should give values ​​1, 1.5, 2, 2.5, 3, 3.5 ... or maybe linspace values ​​(1,30,59) - - in any case , same problem.

. for.

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

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

Linspace, Iterable.

- , , , , .

: (. , , ! , , , end < start , , .)

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;
    }
}
+4

java, . , , MATLAB, MATLAB:

function result = mylinspace(min, max, points)  
answer = zeros(1,points);  
    for i = 1:points  
answer(i) = min + (i-1) * (max - min) / (points - 1);  
end  
result = answer;  

linspace , java-:

public static double[] linspace(double min, double max, int points) {  
    double[] d = new double[points];  
    for (int i = 0; i < points; i++){  
        d[i] = min + i * (max - min) / (points - 1);  
    }  
    return d;  
}  

-, , .

+3

?

for( int number = 1; number <= 30; ++number )

, 3, :

for( int number = 1; number <= 30; number += 3 )

for , - , , .

+2

, MatLab Linspace. Java . , , . , , .

, , .

public static List<Double> linspace(double start, double stop, int n)
{
   List<Double> result = new ArrayList<Double>();

   double step = (stop-start)/(n-1);

   for(int i = 0; i <= n-2; i++)
   {
       result.add(start + (i * step));
   }
   result.add(stop);

   return result;
}
+1

, , , Linspace.

// If you write linspace(start,end,totalCount) in Matlab ===>

for(float i = start; i < end; i += (end-start)/totalCount)
    something(i);
0

, Matlab, Java :

>>> st=3;ed=9;num=4;
>>> linspace(st,ed,num)

ans =

     3     5     7     9
>>> % # additional points to create (other than 3)
>>> p2c=num-1;
>>> % 3 is excluded when calculating distance d.
>>> a=st;
>>> d=ed-st;
>>> % the increment shall calculate without taking the starting value into consideration.
>>> icc=d/p2c;

>>> for idx=[1:p2c];
a(idx+1)=a(idx)+icc;
end;
>>> a

a =

     3     5     7     9

>>> diary off
0

All Articles