Random numbers generation based on specific distribution function, then sort in specific way

18 views (last 30 days)
I would like to generate random numbers based on some distribution function in Matlab. After generating numbers, sort them in a specific way. For example, during rush hour, the demand for taxis is high. Thus, after the generation of demand numbers, put the highest number in the specific time, and then sort other numbers from the highest number.
For instance:
(t=1) 10;
(t=2) 12;
(t=3) 16;
(t=4) 21;
(t=5) 25;
(t=6) 22;
(t=7) 18;
(t=8) 15;
(t=9) 11;
(t=10) 7
Finally, export data to Excel.

Answers (2)

the cyclist
the cyclist on 8 Jun 2023
There are many ways to generate random numbers in MATLAB.
To write to Excel, you can use writetable.
Do you need the highest numbers in the specific time (and decreasing away from there) always, or just statistically? Drawing numbers from a Poisson distribution will have this property in the long run, but not necessarily for every possible draw. For example:
rng default
t = poissrnd(6,157,1);
histogram(t)
You can see that there are more 9's than 8's. But on average there will be more 8's than 9's. This type of randomness is what will more typically observed in the real world. But I don't really know what you need.
(Out of curiosity, is this a school assignment? Sounds like a school assignment.)
  1 Comment
Maho
Maho on 8 Jun 2023
Thank you so much for your response. Noteworthy, It's not a school assignment.
I think that I couldn't explain my problem correctly!
I would like to generate random numbers; for example, random('Exponential',0.2, 24, 1). Now, in clum of random numbers, I would like to sort them. (In this case 24 means 24 hours). I want to put the highest random number at 8 p.m. and then sort other numbers from it (soem of them after this time and other before).

Sign in to comment.


John D'Errico
John D'Errico on 8 Jun 2023
Edited: John D'Errico on 8 Jun 2023
Where is the problem?
  1. You don't say what the distribution of those numbers is, so absolutely nothing can be done until you decide WHICH distribution they come from.
  2. Once you know the distribution, then generate them. Again, what is the problem? There are many tools in MATLAB that can be used to generate random numbers, so rand, randi, randn, and then over a dozen more in the stats toolbox.
  3. Then you need to sort them. Use sort. Again, what is the problem? You don't say what the criteria for how this sorting should work, only somethign vague.
  4. Finally, you need to export them. A better suggestion is to learn MATLAB sufficiently well that you can just work in MATLAB. If your goal is to work in Excel, then there are random number gneeration tools in Excel too, so just work there, and don't bother with MATLAB.
Break a large problem for you into small problems. Learn to solve each small problem, then put it all together.
  3 Comments
the cyclist
the cyclist on 8 Jun 2023
If we set aside all additional, irrelevant information you provided, it seems like your question is, "Given a list of numbers, how can I sort them so that the largest is at a specified index, and then progressively smaller in both directions?"
For example, how to go from
x = [17 2 3 5 11 7 13];
to
xs = [3 7 13 17 11 5 2];
Can I suggest that you post a new question, asking just that, without the distracting info. You will get more people looking at a new question.
Maho
Maho on 8 Jun 2023
Thank you so much. I asked another question based on your advice.
https://www.mathworks.com/matlabcentral/answers/1980384-sorting-numbers-that-the-largest-is-at-a-specified-index-and-then-progressively-smaller-in-both-dir

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!