Discussion:
build 1D array dynamically with data from for loop
(too old to reply)
Merida
2008-08-07 08:10:10 UTC
Permalink
Hi I would like to build a 1D array dynamically with data coming out of a for loop:I have data coming out of a for loop which fills a 1D array with 10 rows - there are 100 of these coming out of the for loop as defined by the index on the loop. (ofcourse the results of the 100 iterations are all different)Now, i want to concatenate the results into a single 1D array, with the result going one under the next. I want to do this dynamically as the results come out of the for loop, so that finally I want to have a 1D array with 1000 rows showing the (10 x 100 results), i.e. 100 lots of the 1D array with 10 rows each from the for loop, one under the next.Can someone suggest how to do this? Cheers!
MikeS81
2008-08-07 08:10:11 UTC
Permalink
Hi Merida,
use a shiftregister on your for loop. The easiest way is to use the build array function in each iteration with option (concatenate inputs), but it would be much better if you first initalized an array with the size you need. Use than the replace array function inside the loop.
The attached picture shows both solutions.
Hope it helps.Mike


forloops.PNG:
Loading Image...
smercurio_fc
2008-08-07 16:40:05 UTC
Permalink
Another simple solution is to simply auto-index and reshape afterwards:<img src="Loading Image..."> In terms of memory management the best solution is to pre-allocate. The auto-indexing/reshape next, and the Build Array method the worst. Message Edited by smercurio_fc on 08-07-2008 11:15 AM


Example_VI_BD6.png:
http://forums.ni.com/attachments/ni/170/347601/1/Example_VI_BD6.png
Cory K
2008-08-07 19:40:05 UTC
Permalink
I would agree with smercurio.You dont want to have to deal with 2 steps, 1 building the array and another reshaping.You would rather only have&nbsp;to do 1.
His example works really well, and you can change the number of itereations of each for loop according to the size of your final&nbsp;2d array.
Merida
2008-08-08 00:40:15 UTC
Permalink
Hi MikeS81, Smercurio and CoryK, thank you for your attempts and nice ideas. There is still one problem, with your examples I get a repeated 10 row pattern all the way down the 1D array for each iteration of the loop. The data coming out of my for loop at each iteration is different, so the first 10 rows that come out will be different to the next 10 rows, which will be different again to the next 10, etc. That is, the data coming out of my loop changes with each iteration and I want to stack this data into a 1D array. With your example codes, what I get is the following repeated pattern for each iteration (for example):1st iteration: 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3...etc. 2nd iteration: 0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8...etc3rd iteration: 5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7...etc4th iteration, 3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5...etc...etc, etc. Following this example, what I want to get is a 1D array with the results from each iteration stacked on top of each other:1,2,3,4,5,6,7,8,9,0,0,9,8,7,6,5,4,3,2,1,5,6,7,8,9,0,1,2,3,4,3,4,5,6,7,8,9,0,1,2,...etc.|---1st iteration--|---2nd iteration---|---3rd iteration---|---4th iteration---|...5th iteration...etc, etcI have been trying various things and although it seems like it should be quite easy, I am haveing real difficulties with this...can you (or someone) suggest how to do this please?Thanks again for your effortsCheers!
Merida
2008-08-08 00:40:15 UTC
Permalink
Hi MikeS81, Smercurio and CoryK, thank you for your attempts and nice ideas. There is still one problem, with your examples I get a repeated 10 row pattern all the way down the 1D array for each iteration of the loop. The data coming out of my for loop at each iteration is different, so the first 10 rows that come out will be different to the next 10 rows, which will be different again to the next 10, etc. That is, the data coming out of my loop changes with each iteration and I want to stack this data into a 1D array. With your example codes, what I get is the following repeated pattern for each iteration (for example):1st iteration: 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3...etc. 2nd iteration: 0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8...etc3rd iteration: 5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7...etc4th iteration, 3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5...etc...etc, etc. Following this example, what I want to get is a 1D array with the results from each iteration stacked on top of each other:1,2,3,4,5,6,7,8,9,0,0,9,8,7,6,5,4,3,2,1,5,6,7,8,9,0,1,2,3,4,3,4,5,6,7,8,9,0,1,2,...etc.|---1st iteration---|---2nd iteration---|---3rd iteration---|---4th iteration---|, etcI have been trying various things and although it seems like it should be quite easy, I am haveing real difficulties with this...can you (or someone) suggest how to do this please?Thanks again for your effortsCheers!
Ravens Fan
2008-08-08 03:40:06 UTC
Permalink
Merida wrote:There is still one problem, with your examples I get a repeated 10 row pattern all the way down the 1D array for each iteration of the loop.


I don't see how that could be happening with Smercurio's code.&nbsp; His just generates a random number on each and every iteration of the inner most for loop.&nbsp; I would recommend that you post your VI.&nbsp; You must be having a problem with how your are implementing his example.
&nbsp;
There almost seems to be a pattern as to the sequences you are creating, but it seems to fall apart in the 3rd - 4th iteration.&nbsp; 1 to 0 then 0 to 1, then it jumps to 5?
&nbsp;
Please post your code and a clearer description of what you want your sequence to look like.
&nbsp;
The example seems to answer your question of how to combine the results of the 2-D array into a single 1-D array.&nbsp; But now you seem to have a different question about how to generate the initial sequences into the 2-D array.
Merida
2008-08-08 04:40:04 UTC
Permalink
Ravens Fan, I really don't see the usefulness of your response - i think you misunderstand:1. I am not creating any sequences. Please read the post carefully. I don't understand what you mean that '...it falls apart in the 3rd - 4th iteration...etc.' The example I gave shows exactly what I get. (Note that the separation lines where I have |---1st iteration---|---2nd iteration---| etc. moved when I posted the reply. In the example I gave, the first iteration produces 1,2,3,4,5,6,7,8,9,0; the 2nd produces 0,9,8,7,6,5,4,3,2,1, the 3rd produces 5,6,7,8,9,0,1,2,3,4 the 4th produces 3,4,5,6,7,8,9,0,1,2...etc2. If I could post the code, I would have done it - the problem is that the algorithm that produces these results is huge and full of sub vis etc. - hence I tried to describe with the example.3. Obviously the example does not answer my question - if it did, I would not have posted a reply asking for more help!4. I really do not know what you are referring to when you speak about 2D arrays - nowhere in my questions do I mention 2D arrays! ???? I do not have a different question!!!- Please read the post carefully. Here I have some more to clarify:I have an algorithm in a for loop and for each iteration of this loop (which depends on the size of my data and indexing on this for loop) it produces predictions of variable y in a 1D array with 10 rows. So, for each iteration, the results will be different. Now, I want to stack these results in a 1D array.I have a for loop which produces results in a 1D array - the results will differ for each iteration of the loop - I then used the examples sent by Smercurio et al. and what I get is results for each iteration of the loop that are repeated in all 1000 rows of the 1D array.Cheers.
MikeS81
2008-08-08 07:40:05 UTC
Permalink
Hi Merida,
if you use my example, then you will have definitifly a 1D array. I donĀ“t understand the problem you have. Can you create an example vi without your algorithmus, but which contains the values you get and the result you need?
&nbsp;
Mike
Merida
2008-08-11 12:10:06 UTC
Permalink
Hi guys, I took the weekend off and returned to look at your examples again - you are right...they work. My code needs a little bit of 'cleaning up' and i missed something in all that. Thank you for your help and I will be sure to rate your help!CheersM.
Merida
2008-08-11 12:10:06 UTC
Permalink
Hi guys, I took the weekend off and returned to look at your examples again - you are right...they work. My code needs a little bit of 'cleaning up' and i missed something in all that. Thank you for your help and I will be sure to rate your help!CheersM.
Loading...