Giroud2
Giroud2

Reputation: 45

MATLAB - Generate a random matrix taking different strings from other ararys

I have 3 different arrays of strings in Matlab, these contain different departure times.

One array is for departures at '07:00', '07:10'..... The next arrays is for departures at '08:00','08:10'... And the third is for departures at '09:00','09:10'..

Now I want to create a new matrix/array take take random values from these 3 arrays and puts it into a new matrix with 1000 inputs, how can i do this?

code:

SevenOclock = ["07:00","07:05","07:10","07:15","07:20","07:25","07:30","07:35","07:40","07:45","07:50","07:55"]'
EightOclock = ["08:00","08:05","08:10","08:15","08:20","08:25","08:30","08:35","08:40","08:45","08:50","08:55"]'
NineOclock  = ["09:00","09:05","09:10","09:15","09:20","09:25","09:30","09:35","09:40","09:45","09:50","09:55"]'

randomDeparture = rand(SevenOclock; EightOclock; NineOclock) %NOT WORKING!! HELP

Could someone plz help me?

Upvotes: 0

Views: 50

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

You are looking for randsample, to sample randomly. The code, tidied up, would look like this:

minutes = ["00";"05";"10";"15";"20";"25";"30";"35";"40";"45";"50";"55"];

SevenOclock  = "07:" + minutes;
EightOclock  = "08:" + minutes;
NineOclock   = "09:" + minutes;

allDepartures=[SevenOclock;EightOclock;NineOclock];

randomDeparture = randsample(allDepartures,1000,true);

Upvotes: 2

Related Questions