Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
RandomSample[] supports weighted picks –  belisarius May 27 at 0:59
    
Test for example RandomSample[Range[30]^4 -> Range[30], 30] –  belisarius May 27 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. –  Oleksandr R. May 28 at 1:32
    
thank you so much for your help but I cant explain more obviously. –  mostafa May 28 at 9:23

1 Answer 1

up vote 2 down vote accepted
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}} *)
share|improve this answer
    
This is a so good way, I thank you so much. It was wonderful. –  mostafa May 29 at 6:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.