# Matrix and random, weighted assignment of rows

I asked a question in this category previously but my problem has changed.

On one hand, I have a large number of data files as .dat format, and on the other hand, I have a matrix A which must be filled with these data. Each of rows of the A must be devoted to each of the data files BUT NOT WITH THE SAME CHANCE. Actually, my desire is to put each of data files in each row (after importing them) but in a RANDOM WAY.

For example (just for example here, there are 5 data files): (the number of the row of A is the same of the number of data files and the number of the column of that is fixed to 10). However, in the elements shown below, n has 11 because, as I will explain, I don't want to add the first element from a data file to the matrix.

m = {8, 1, 5, 3};
n = {6, 2, 7, 11, 3, 8, 10, 12, 14, 1, 4};
k = {10, 3, 6, 8, 2};
S = {7, 1, 9};
R = {9, 4, 6, 1, 5};


I mean, the k is the most significant relative the other elements and must have the highest chance of getting the first row of the matrix;

For example with the weight 10 (just for example). After k, R (with the weight 9) must have the next highest chance to get the first row of the matrix. Then with decreasing chance: m with weight 8. S with weight 7, and so on. Every previously assigned weight of each data element is given in the first element of the element.

For instance, n might be assigned to the second row even though n has the less chance than R to occupy the second row. As a matter of fact, the random generator, giving attention to the weights, will decide which data element to assign to which row.

• RandomSample[] supports weighted picks May 27, 2014 at 0:59
• Test for example RandomSample[Range^4 -> Range, 30] May 27, 2014 at 1:02
• All your caps and bold text don't make the question any clearer. In fact, I can't really understand what you're asking for at all. If it is just a weighted random sample from given elements, then this is about the most convoluted way to describe it that I can imagine. If your question involves more than that, you'll really need to clarify it substantially. May 28, 2014 at 1:32
• thank you so much for your help but I cant explain more obviously. May 28, 2014 at 9:23

m = {8, 1, 5, 3};
n = {6, 2, 7, 11, 3, 8, 10, 12, 14, 1, 4};
k = {10, 3, 6, 8, 2};
s = {7, 1, 9};
r = {9, 4, 6, 1, 5};

(* Using the first elements of the lists as the relative weights : *)
RandomSample[First /@ # -> Rest /@ # &[{m, n, k, s, r}]]
(*{{4,6,1,5},{3,6,8,2},{1,5,3},{1,9},{2,7,11,3,8,10,12,14,1,4}} *)

RandomSample[First /@ # -> Rest /@ # &[{m, n, k, s, r}]]
(* {{3,6,8,2},{1,9},{4,6,1,5},{1,5,3},{2,7,11,3,8,10,12,14,1,4}} *)


To form a 5x10 matrix with randomly selected rows:

a = PadRight[RandomSample[First /@ # -> Rest /@ # &[{m, n, k, s, r}]]]
(* {{1, 9, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 5, 3, 0, 0, 0, 0, 0, 0, 0},
{3, 6, 8, 2, 0, 0, 0, 0, 0, 0}, {2, 7, 11, 3, 8, 10, 12, 14, 1, 4},
{4, 6, 1, 5, 0, 0, 0, 0, 0, 0}} *)

• This is a so good way, I thank you so much. It was wonderful. May 29, 2014 at 6:59