How can I produce linearly and logarithmically spaced arrays in C?

In summary, to produce linearly and logarithmically spaced arrays in C, you can use a for loop and increment by the desired linear or log value. For logarithmic spacing, you can use the formula float step = pow(10.0 / array[0], 1./11.); to calculate the step size.
  • #1
ShaneXavier
6
0
For the use of linear and logarithmic interpolation, how can I produce linearly and logarithmically spaced arrays in C? I know in Matlab the code is

A = linspace(a,b,c) or A = logspace(a,b,c)

but how can I do this in C?
 
Technology news on Phys.org
  • #2
ShaneXavier said:
how can I produce linearly and logarithmically spaced arrays in C?
You use a for loop, an initial value, and you increment by the the linear or log value you want, then place those values in the array (which is basically what MATLAB is doing in the background)

basically some variant of:
int spaced_array[NUMVALUES];
int i=initial linear/log value;
for (j=0; j<NUMVALUES; j++)
{
spaced_array[j]=i;
i+=linear/log increment;
}
 
Last edited:
  • #3
Thanks for your help. I am having difficulties in understanding something else. If I have an initial value say log(2) and my final value of 10, and I want 12 logarithmically distributed samples, do I say is my step size going to be (10 - log(2))/12? I am kind of confused about how to implement this.

And I assure you, this is not homework.
 
  • #4
float array[12];
array[0] = log(2.0);
float step = pow(10.0 / array[0], 1./11.);
for(j=0; j<11; j++)
array[j+1] = array[j] * step;
 
  • #5


In C, there are a few ways to produce linearly and logarithmically spaced arrays. One option is to use the "for" loop to iterate through the desired range and increment the values by a specific amount. For example, to produce a linearly spaced array from 1 to 10 with 10 elements, you can use the following code:

float A[10];
float increment = (10 - 1) / (10 - 1); //calculate the increment
for(int i=0; i<10; i++){
A = 1 + i * increment; //calculate the values for each element
}

To produce a logarithmically spaced array, you can use the "pow" function to calculate the values at each index. For example, to produce a logarithmically spaced array from 1 to 100 with 10 elements, you can use the following code:

float A[10];
float base = 10;
for(int i=0; i<10; i++){
A = pow(base, 1 + (i * (2 - 1) / (10 - 1))); //calculate the values for each element
}

Another option is to use the "malloc" function to dynamically allocate memory for the array and then use the "memset" function to fill the array with desired values. However, this method may be more complex and require more advanced knowledge of C programming. It is recommended to consult C language references or seek assistance from a professional programmer for more detailed instructions.
 

1. How do you create a linearly spaced array in C?

To create a linearly spaced array in C, you can use the linspace() function from the math.h library. This function takes in three arguments: the starting value, the ending value, and the number of elements you want in the array. It then evenly spaces the values between the starting and ending values to create a linearly spaced array.

2. Can you specify the step size when creating a linearly spaced array in C?

Yes, you can specify the step size when using the linspace() function. The function takes in a fourth argument for the step size, which determines the spacing between each element in the array. By default, the step size is calculated based on the number of elements and the range between the starting and ending values.

3. How do you access individual elements in a linearly spaced array in C?

To access individual elements in a linearly spaced array in C, you can use the array index notation. For example, if you have an array named myArray, you can access the first element using myArray[0], the second element using myArray[1], and so on. The index starts at 0 and goes up to the number of elements minus one.

4. Can you modify elements in a linearly spaced array in C?

Yes, you can modify elements in a linearly spaced array in C by using the array index notation. You can assign a new value to an element by using the assignment operator =. For example, myArray[0] = 10; will change the first element in the array to 10.

5. How do you print a linearly spaced array in C?

To print a linearly spaced array in C, you can use a for loop to iterate through each element and print it. The loop should start at 0 and go up to the number of elements minus one. Inside the loop, you can use the array index notation to access and print each element. You can also use the printf() function to format the output.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
895
  • Programming and Computer Science
Replies
1
Views
415
  • Topology and Analysis
Replies
14
Views
340
  • Programming and Computer Science
Replies
25
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
713
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top