5

To generate a random matrix, I have used the following:

x = randi([-3,3],4)

However I don't want any zeros in my output matrix. How can I do this?

Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
newzad
  • 706
  • 1
  • 14
  • 29

4 Answers4

8

Create random integers between -3 and 2, but replace the zeros with 3s:

x = randi([-3,2],4);x(x==0)=3
David
  • 8,449
  • 1
  • 22
  • 32
7

@David provides a neat solution for your specific problem. But the following solution will work for any set of numbers (note, they don't need to be integers), although I've set it up for your specific problem.

OutputSize = [20, 1]; %Size of output matrix
A = [-3; -2; -1; 1; 2; 3]; %Vector of numbers you want to sample from (can be whatever you want)
x = A(randi(length(A), OutputSize)); %We randomly select the indices of A

You might want to turn the above into a function with inputs A and OutputDim.

You could even re-sample elements of a cell array using this method...

EDIT: Adjusted the code to allow output array of any size following the suggestion of @Divakar in the comments

Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
4

You can use an iterative approach to check for zeros and replace them with a smaller set of randomized elements within that same interval [-3,3]. We keep on doing this, until there are no zeros left.

The code to achieve such an approach would be something like this -

N = 4; %// datasize
x1 = randi([-3,3],N); %// the first set/iteration of random numbers

done = false; %// flag to detect if any zeros are left after iterative substitution
while ~done %// keep the loop running until no zeros are found
    x1((x1==0)) = randi([-3,3],numel(find(x1==0)),1); %// substitute zeros with 
                                               %// smaller set of random numbers
    done = all(x1(:)~=0);
end
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • 1
    Is this correct? I mean, the probability plots. The solution by David crates numbers with equal probability, and then changes one of "index" or value. He shouldn't have higher amount of 3's. right? Or I am missing something? – Ander Biguri Oct 31 '14 at 10:47
  • @AnderBiguri Yeah the second plot is the one with David's approach. – Divakar Oct 31 '14 at 10:48
  • 1
    I see, but I am trying to understand ( I really suck at statistics) why does he have higher amount of 3's. – Ander Biguri Oct 31 '14 at 10:49
  • 1
    @AnderBiguri Ahhh! My bad !! Well I assumed David meant this - `randi([-3,3],4)` and then replaced the zeros with `threes`. Damn I need to edit my solution on those now! Thank you for the heads up there! – Divakar Oct 31 '14 at 10:53
  • @Divakar You had me worried for a few seconds! – David Oct 31 '14 at 11:11
  • @David Sorry my bad really there! Edited the solution! :) +1 for your solution, it looks smart one now :) – Divakar Oct 31 '14 at 11:34
2

Probably not the fastest solution, but there's a randsample function in the Statistics Toolbox that does just that:

m = 4; %// number of rows
n = 4; %// number of columns
values = [-3:-1, 1:3]; %// values to sample from
x = reshape(randsample(values, m*n, true), [m n]);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147