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

  • Thread starter Thread starter ShaneXavier
  • Start date Start date
  • Tags Tags
    Data Linearly
Click For Summary

Discussion Overview

The discussion focuses on generating linearly and logarithmically spaced arrays in the C programming language, comparing approaches to those available in Matlab. Participants explore methods for implementing these concepts programmatically.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about producing linearly and logarithmically spaced arrays in C, referencing Matlab functions as a comparison.
  • Another participant suggests using a for loop with an initial value and incrementing by a linear or logarithmic value to fill an array, indicating this is similar to Matlab's internal processes.
  • A participant expresses confusion regarding the calculation of step size for logarithmically spaced samples, specifically questioning the formula to use when transitioning from log(2) to 10 over 12 samples.
  • A later reply provides a code snippet for generating logarithmically spaced values, utilizing the logarithm of the initial value and a calculated step based on the final value.

Areas of Agreement / Disagreement

Participants generally agree on the basic approach to creating spaced arrays but express differing levels of understanding regarding the implementation details, particularly for logarithmic spacing. The discussion remains unresolved regarding the optimal method for calculating step sizes.

Contextual Notes

There are limitations in the clarity of the step size calculation for logarithmic spacing, as well as potential misunderstandings about the implementation in C compared to Matlab.

ShaneXavier
Messages
6
Reaction score
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
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:
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.
 
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;
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K