1

I've a matrix of order 100*10 . Now the objective is to fill each columns of the matrix with random integer within a specific range. Now the problem is for every column the range of the random number changes. For instance, for the first column, the range is [1,100] , for the second its -10 to 1 and so on till 10th column.

This is what I've tried:

b = [0,100;-10,1;0,1;-1,1;10,20]
a = []
for i=1 to 10
    a[] = [(i:100)' randi(1,100)]
end

How do I generate a matrix of this form?

OBX
  • 6,044
  • 7
  • 33
  • 77

1 Answers1

2

I don't have matlab installed right now, but i would do something like this.

m = 100;
n = size(b, 1);
range  = b(:, 2) - b(:, 1);
offset = b(:, 1);

A = round(bsxfun(@minus, bsxfun(@times, rand(m, n), range), offset);

Without loop it would become:

M = 100;
N = size(b, 1);

A = zeros(m, n); % preallocate to avoid matrix expansion
for ii = 1:n
    A(:, ii) = randi(b(ii,:), m, 1);
end
Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
Michael H.
  • 535
  • 6
  • 11