-2

How can I randomize and generate numbers from 0-50 in matrix of 5x5 with SUM or each row printed on the right side?

+ is there any way to give weight to individual numbers before generating the numbers?

Please help

Thanks!

  • Please clarify what you're trying to achieve (in the question, not in comments) and show us the code you've written so far and how the output differs from your desired output. – beaker Aug 03 '17 at 16:54

2 Answers2

1

To generate a random matrix of integers between 0 and 50 (sampled with replacement) you could use

M = randint(5,5,[0,50])

To print the matrix with the sum of each row execute the following command

[M sum(M,2)]

To use a different distribution there are a number of techniques but one of the easiest is to use the datasample function from the Statistics and Machine Learning toolbox.

% sample from a truncated Normal distribution. No need to normalize
x = 0:50;
weights = exp(-0.5*(x-25).^2 / 5^2);
M = reshape(datasample(x,25,'Weights',weights),[5,5])

Edit:

Based on your comment you want to perform random sampling without replacement. You can perform such a random sampling without replacement if the weights are non-negative integers by simulating the classic ball-urn experiment.

First create an array containing the appropriate number of each value.

Example: If we have the values 0,1,2,3,4 with the following weights

  • w(0) = 2
  • w(1) = 3
  • w(2) = 5
  • w(3) = 4
  • w(4) = 1

Then we would first create the urn array

>> urn = [0 0 1 1 1 2 2 2 2 2 3 3 3 3 4];

then, we would shuffle the urn using randperm

>> urn_shuffled = urn(randperm(numel(urn)))

urn_shuffled =

     2     0     4     3     0     3     2     2     3     3     1     2     1     2     1

To pick 5 elements without replacement we would simple select the first 5 elements of urn_shuffled.

Rather than typing out the entire urn array, we can construct it programatically given an array of weights for each value. For example

weight = [2 3 5 4 1];
urn = []
v = 0
for w = weight
    urn = [urn repmat(v,1,w)];
    v = v + 1;
end

In your case, the urn will contain many elements. Once you shuffle you would select the first 25 elements and reshape them into a matrix.

>> M = reshape(urn_shuffled(1:25),5,5)
jodag
  • 19,885
  • 5
  • 47
  • 66
  • Hi Thanks for helping :) here is example of the problem in the picture below. numbers are non repeating and shud be based on the frequency of the table on the right. https://ibb.co/h2Rj2F – Gajendra Badwal Aug 03 '17 at 06:46
  • You can perform uniform sampling without replacement using `randsample` but it doesn't support weighted random sampling without replacement. see https://www.mathworks.com/help/stats/randsample.html – jodag Aug 03 '17 at 06:51
  • @GajendraBadwal Clarifications should be added to the question itself for everyone to see. – beaker Aug 03 '17 at 16:52
  • @GajendraBadwal Added a manual method to perform random sampling without replacement that should work for your case. – jodag Aug 03 '17 at 17:18
0

To draw random integer uniformly distributed numbers, you can use the randi function:

>> randi(50,[5,5])
ans =
34    48    13    28    13
33    18    26     7    41
 9    30    35     8    13
 6    12    45    13    47
25    38    48    43    18

Printing the sum of each row can be done by using the sum function with 2 as the dimension argument:

>> sum(ans,2)
ans =
136
125
 95
123
172

For weighting the various random numbers, see this question.

jvz
  • 1,183
  • 6
  • 13