01-30-2010 06:44 AM - edited 01-30-2010 06:46 AM
Hi!
I have a 2-D array with m rows and n columns: ((x11,x12,x13,...,x1n),(x21,x22,x23,...,x2n),(x31,x32,x33,...,x3n),...,(xm1,xm2,xm3,...,xmn)), where xij means the element located in row i and column j, and xij is a real number.
I need to fill each row with a previously defined number of zeros per row and to the beginning of each row. For example 5 zeros have to be added to the beggining of the first row, two to the second row, 4 to the third row,...,6 to the last row:
((0,0,0,0,0,x11,x12,x13,...,x1n,0),(0,0,x21,x22,x23,...,x2n,0,0,0,0),(0,0,0,0,x31,x32,x33,...,x3n,0,0),...,(0,0,0,0,0,0,xm1,xm2,xm3,...,xmn))
I added zeros to the end to maintain row's lenghts equal between them
May you please help me with the code?
Solved! Go to Solution.
01-30-2010 08:18 AM
Hi fgarcia,
Is there allready data in the 2D array which you want to add the zeros in front of? Or is the array empty and you want to start adding data to it with a predefined number of zeros at the front of each row?
Steve.
01-30-2010 10:14 AM
Hi Steve:
Yes, there is already data.
Thanks!
Francisco
01-30-2010 10:15 AM - edited 01-30-2010 10:16 AM
If you autoindex the 2D array and concatenate each row to a zero filled 1D array of defined lenght, then autoindex at the right boundary to again form a 2D array, the rows will be padded correctly automatically.
Here is some simple example code:
01-30-2010 10:59 AM - edited 01-30-2010 11:01 AM
It works very well in my VI. Thank you very much!
01-30-2010 03:19 PM
The code shown is OK for occasional use and small arrays, but if you are dealing with very large data structures, you get a relatively large memory management penalty due to the constantly resizing data structures.
If performance is an issue, it would be better to calculate the final 2D array size beforehand, initialize it with all zeroes, and then replace elements with the old data rows at the correct offsets. It would not be much more complicated. Try it! 🙂