theKingDidier
theKingDidier

Reputation: 7

Create matrix with n elements from an array

Is there a way to create matrix that is consisted of a certain numbers that are stored in an array? For example, I want to create a 10-by-1 matrix consisting only of numbers from an array a = [6,2,15,24], that are randomly stored in matrix elements. The final product should look something like this:

M = [15,24,2,15,2,6,24,15,2,15]

Upvotes: 0

Views: 145

Answers (2)

horchler
horchler

Reputation: 18504

If you have the Statistics toolbox, you can use randsample with the third argument set to true to indicate that the data a is to be sampled with replacement:

a = [6 2 15 24];
M = randsample(a,10,true)

Upvotes: 3

jez
jez

Reputation: 15369

function b = resample( a, size )
indices = randi( numel( a ), size );
b = a( indices );

Example:

>> resample( [6,2,15,24],  [4,5] )
ans =

     2     6    15     2     6
     2     2    15    15     6
    24     6     6     2     6
     2     6    24     6     2

Upvotes: 0

Related Questions