LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Building an evenly spaced array

Hopefully this is a question that has an easy (read "elegant") solution.

There have often been times where I need an evenly spaced numeric array between two numbers.  For example, I may want an array that goes as:
1,2,3,4,...100
or I want an array that goes as:
0.7,0.8,0.9,...,5.4

I have always built these up using for loops.  Is there anyway to process this in a single array computation?  A matlab equivalent would be:
A=[0.7:0.1:5.4];
for the bottom array.  But I don't want to call Matlab everytime I do this.  I don't have LV8, but can someone tell me if the MathScript node will do this in a more efficient manner than the serial loop?  Any ideas about this in lower versions?

-Robert
0 Kudos
Message 1 of 20
(8,382 Views)
Attached is a labview equiv.  This should execute fast since it uses autoindexing and a perdetermined array size.  I didnot include bounds checking so sont set the step size to 0.
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 2 of 20
(8,363 Views)
Here is the attached vi in labview 7.0 format
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 3 of 20
(8,364 Views)

Is there a more efficient method of population?  this will run in O(n) time, it might be possible to use some sort of single instruction multiple data call but I am not sure about this.  In machine code on a single processor you would allocate the memory and populate it by index requiring O(n).  I would be cutious to see a faaster method (pentium processor).

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 4 of 20
(8,363 Views)
I think if all inputs to the loop are constants, the compiler may pre-calculate the values so that the loop is never actually called.

Otherwise, if you're manipulating arrays taking up less then 64kb of memory, the generation will run in the L1 cache of most modern processors, meaning it will run really fast, so the time factor is practically non-existant.

Just my take on it,

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 5 of 20
(8,353 Views)

Hi Robert, this is _pretty_ simple

On second thought, falkpl's is better (never mind...)

Message Edited by Dynamik on 11-16-2005 11:25 AM

Message Edited by Dynamik on 11-16-2005 11:29 AM

When they give imbeciles handicap-parking, I won't have so far to walk!
0 Kudos
Message 6 of 20
(8,350 Views)
A few responses:
@falkpl : Thanks.  I was actually looking for an *alternative* to this method.
@shoneill : Interesting concept and I wish it were true, but I believe falkpl is right.  For a larger arrays take longer to build.  I graphed this in the attached picture.
0 Kudos
Message 7 of 20
(8,338 Views)
Thanks Dynamik.  This is essentially the same method as falkpl's.  Again, I am hoping to look for a more clever way to do it.  My first thought was that if there was a simple way to build an indexed array [1,2,3,4...] then your technique (array multiplied by a scalar) would be a solution.   Computation-wise, it is slightly more efficient than falkpl's (see attached graph for comparison)...  But I am looking for something that does the full matrix build instead of a serial build.

This is more for the sake of curiousity.
0 Kudos
Message 8 of 20
(8,315 Views)
It might be slightly simpler to use add and a shift tregister in the loop.
 
You could also use the Ramp pattern (LabVIEW full or higher).
 
Attached is an example (LabVIEW 7.0).
 
 
(I haven't done any benchmarking, but NO, LabVIEW does not precompute the loop and fold the output into a constant. Still, these simple loops are fast and efficient and probably much faster than anything else you do with that array later.)

Message Edited by altenbach on 11-16-2005 10:15 AM

Download All
Message 9 of 20
(8,312 Views)
Thanks ( 🙂 ) for the feedback, Robert.
 
To be fair to falkpl, he is doing the offset too.
 
Cheers (and BRAVO! Again (dammit) Altenbach! )

Message Edited by Dynamik on 11-16-2005 12:24 PM

When they give imbeciles handicap-parking, I won't have so far to walk!
0 Kudos
Message 10 of 20
(8,310 Views)