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

  • Thread starter Thread starter ShaneXavier
  • Start date Start date
  • Tags Tags
    Data Linearly
AI Thread Summary
To produce linearly and logarithmically spaced arrays in C, a for loop is typically used alongside an initial value and an increment. For linear spacing, an array is filled by incrementing a starting value by a fixed linear step size. For logarithmic spacing, the process involves calculating the step size based on the logarithmic scale. In the example provided, to create 12 logarithmically distributed samples starting from log(2) to 10, the step size is determined using the formula (10 - log(2))/12, although this approach can be confusing. The correct implementation involves initializing the first element of the array with log(2) and calculating the step size using the formula pow(10.0 / array[0], 1./11.) to ensure proper logarithmic spacing. The loop then populates the array by multiplying the previous value by the calculated step size. This method effectively mimics MATLAB's linspace and logspace functions in C.
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;
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top