SteveMyres Posted October 15, 2011 Share Posted October 15, 2011 What's the cleanest way to fill a 2D from a 1D array with the corresponding number of elements? Say a 1x30 array to a 5x6? Loop in a sequence? Or is there some array manipulating function that can do this in one shot? Link to comment Share on other sites More sharing options...
AzeoTech Posted October 17, 2011 Share Posted October 17, 2011 You can't do this in one shot, but you can do it in 5 or 6 shots instead of 30 by using transpose. Lets assume x is your 1x30 array and y is your 5x6 (I'm going to assume also that 1 and 5 are your columns, and 30 and 6 are rows): for (var i = 0, i < 6, i++) y = transpose(x[i*5, i*5+4],1) endfor To do it even faster, do it the other way, but of course that is also changing the ordering: for (var i = 0, i < 5, i++) y[] = x[i*6, i*5+5] endfor Which you use depends on whether the 1x30 is grouped by rows or columns when it becomes 5x6. The best way to see this work is to set x = seqadd(1,1,30) which makes x = {1,2,3,...} and then look at the result of both. Link to comment Share on other sites More sharing options...
SteveMyres Posted October 18, 2011 Author Share Posted October 18, 2011 Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.