0
$\begingroup$

I believe that it may be simplified, but I have no ideas anymore.. I need to flatten inside each element of array. F.ex. {{1}, 128552, 6, 1} ---> {1, 128552, 6, 1}

Input data (Z - requests, t - periods of time):

Z = {{ 1, 3, 3, 7}, {8, 6, 6, 3}, {3, 5, 6, 13}, {15, 15, 9, 4}, {1, 1, 3, 6}, {9, 4, 3, 2}} t = {{129600, 30240}, {30240, 10080}, {10080, 1440}, {1440, 0}} 

I need to generate time of each request, then number of product (1-6), then sort it and give the index number of requst and in the end append 1.

My decision:

MapIndexed[Prepend[Append[#, 1], #2] &, Sort[Flatten[Table[Partition[Append[Riffle[ RandomInteger[t[[i]], Z[[j, i]]], j], j], 2], {j, 6}, {i, 4}], 2], #1[[1]] > #2[[1]] &]] 

It's part of the answer:

{{{1}, 128559, 3, 1}, {{2}, 128229, 6, 1}, {{3}, 127041, 4, 1},....,{{151}, 34, 3, 1}, {{152}, 18, 4, 1}} 

I don't know how to simplify my decision..

$\endgroup$
1
  • 2
    $\begingroup$ Try Flatten[{#2, #, 1}] & instead of Prepend[Append...]& in the first argument of MapIndexed.. $\endgroup$ Commented Mar 31, 2014 at 22:55

1 Answer 1

3
$\begingroup$

There are couple of improvements for your code:

MapIndexed[ Join[#2, #, {1}] &, SortBy[ Flatten[ Table[ Thread@{RandomInteger[t[[i]], Z[[j, i]]], j} , {j, 6}, {i, 4}] , 2], -#[[1]] &] ] 
$\endgroup$
1
  • $\begingroup$ Thanks again! This is much more simple than my code=) $\endgroup$ Commented Mar 31, 2014 at 23:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.