Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I would like to generate a logarithmically spaced vector and a linearly spaced vector. Just like in in MATLAB: linspace and logspace. The code i have written so far is below. the linear-space number generator seems to work well when i call it. I know the logarithm-space number generator is wrong. :( So, it would be nice if someone could help me.

Thanks in advance.

PS: I started writing java code a month ago. So, i am still a java beginner. :)

Java
import java.util.ArrayList;
 
//Create linearly spaced vector
public static ArrayList linearSpace(int start, int end, int count)
{
  float delta = (end - start) / (float)count;
  ArrayList ArrList= new ArrayList();
  
  for(float i = start; i < end; i = i + delta)
  {
    ArrList.add(i);
  }

  return ArrList;
}
 
//Create logarithmically spaced vector
public static ArrayList logSpace(int start, int end, int count)
{
  float delta = (end - start) / (float)count;
 
  ArrayList ArrList= new ArrayList();

  for(float i = start; i < end; i = i + Math.log(i))
  {
    ArrList.add(i);
  }

  return ArrList;
}
Posted
Updated 29-Apr-11 3:59am
v2

Hi,
I've used some time ago a logarithmically spaced vector on 32 bands with Math.E logarithm base.
There was a need in specifying min/max values within the vector, the logarithm base (Math.E is a standard choice, and the number of points)

C#
private static void GenerateLogSpace(int min, int max, int logBins)
{
       double logarithmicBase = Math.E;
       double logMin = Math.log(min);
       double logMax = Math.log(max);
       double delta = (logMax - logMin) / logBins;
       int[] indexes = new int[logBins + 1];
       double accDelta = 0;
       float [] v = new float[logBins];
       for (int i = 0; i <= logBins; ++i)
       {
           v[i] = (float) Math.pow(logarithmicBase, logMin + accDelta);
           accDelta += delta;// accDelta = delta * i
       }
}

Regards
 
Share this answer
 
v3
Comments
The_Real_Chubaka 29-Apr-11 11:00am    
Thank you for your help.

It seems like Math.log() only takes one argument.
But again, you are using the Math.Log() method instead, with a capital 'L'.
Do i have to write that method myself?
Ciumac Sergiu 29-Apr-11 14:24pm    
Hi,
it's a snippet of code written in C#, so in Java it will be a Math.log(). The second parameter is the base of the logarithm (in C# you can explicitly specify the base). In Java you can use Math.log() that returns the natural logarithm (base e) of a double value, so it will be the same as Math.Log(min, Math.E /*E is the Euler's number */).
Ciumac Sergiu 29-Apr-11 14:26pm    
See the updated solution.
The_Real_Chubaka 29-Apr-11 19:29pm    
Oh. OK, Thank. I will try it tomorrow morning. Time for me to go to bed :)
The_Real_Chubaka 30-Apr-11 9:35am    
Thanks,

It works like a charm :)
I even plotted it. Just beautiful!
My attempt (not tested):
Java
import java.util.ArrayList;
 
//Create logarithmically spaced vector
public static ArrayList logarithmicSpace(int start, int end, int count)
{
  
  ArrayList ArrList= new ArrayList();
  
  for(int i = 0; i <= count; i++)
  {
    float f = start * Math.pow( ((float)end)/start, ((float)i)/count));
    ArrList.add(f);
  }
 
  return ArrList;
}
 
Share this answer
 
Comments
The_Real_Chubaka 30-Apr-11 9:40am    
Thanks. It looks interesting. I'm using the first one that worked: solution 1. I'm sure yours also works just fine :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900