Message Boards Message Boards

0
|
9887 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Generate a list with any number

Posted 10 years ago

Can anyone explain how generate the next list with any number...

n=any number that I introduce, follow the same serie

{{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 2}, {2, 1}, {5, 1}...}

Thank's a lot

POSTED BY: Camilo Pacheco
4 Replies

Taken literally it is

In[2]:= pachecoL[n_Integer?Positive] := 
 Block[{x = {{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 2}, {2, 1}, {5, 1}, Missing[]}},
  If[n >= Length[x], x[[-1]], Take[x, n]]
]

In[3]:= pachecoL[5]
Out[3]= {{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}}
POSTED BY: Udo Krause
Posted 10 years ago

For example I introduce number 5 in n and the number give a range of the list

n=5;

{{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}}

POSTED BY: Camilo Pacheco

data = {{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 2}, {2, 1}, {5, 1}};

If you just want to append a pair then use Append or ApendTo

data = Append[data, {8, 4}]

{{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 2}, {2, 1}, {5, 1}, {8, 4}}

data = {{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 
    2}, {2, 1}, {5, 1}};

AppendTo[data, {8, 4}]

{{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 2}, {2, 1}, {5, 1}, {8, 4}}

% == %%%

True

To select (Take) the first n elements of the data use Take[data, n] where n<= Length[data]

data = {{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 
    2}, {2, 1}, {5, 1}};

n = 5; Take[data, n]

{{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}}

If you want to generate a random list of of n pairs

n = 5; RandomInteger[{1, 9}, {n, 2}]

{{9, 4}, {6, 4}, {1, 8}, {6, 3}, {1, 4}}

POSTED BY: Robert Hanlon

Hi Camilo,

I do not understand the question. What do you mean with next list with any number? Do you mean that you want to add another list of to numbers to that list. Something like:

list = {{2, 1}, {3, 1}, {2, 2}, {5, 1}, {2, 1}, {3, 1}, {7, 1}, {2, 3}, {3, 2}, {2, 1}, {5, 1}}

AppendTo[list, {RandomInteger[7], RandomInteger[7]}]

That would add one entry, i.e. a list of two numbers, to the outer list. Or do you want to add another list to that list like so:

Nest[Append[#, RandomInteger[7, Last[Dimensions /@ {list}]]] &, {list}, 3]

which would do the procedure 3 times and add an additional list of the same shape as the first one?

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract