Controlled Random Generator based off of two columns

9 Ansichten (letzte 30 Tage)
Tessa Aus
Tessa Aus am 24 Aug. 2019
Bearbeitet: dpb am 25 Aug. 2019
I have two columns in Table A, the first column is randomly generats values between 10 and 3600 but the second column needs to be randomly generated based on the first columns values. The second column needs to be a value equal to or less than column 1's equivalent row and if the random value is lower and not equal to it, it must be divisible by traffic.R_P.
I tried the below for loop but of course it is generating random numbers not divisible by traffic.R_P. Not sure how to create a controlled random generator with values divisible by another column.
example is
traffic.R_P = [2500 1200 200 400]
traffic.XX = [2500 60 0 10] %CORRECT and Want
traffic.XX = [2400 60 1100 300] %Incorrect and what I am getting from below code
traffic.R_P = randi([1,36],TDT_Total_ID,1)*100; %is working fine and correctly populating
for T_row = 1 : TDT_Total_ID
traffic.XX(R_dith_row,1) = randi([1,(traffic.R_D(T_row,1)/10)],T_row,1)*10;
end
  3 Kommentare
Tessa Aus
Tessa Aus am 24 Aug. 2019
Apologies and good catch you are correct with the population being between 100 and 3600. See below for correction. Traffic.R_P are all values from 100 to 3600 but traffic.XX can be any values from 10 to 3600, hence why in the for loop the values of R_P are divided by 10. And variable A is incorrect it should say table 'traffic'.
traffic.R_P = [2500 1200 200 400]
traffic.XX = [100 200 50 400]
dpb
dpb am 25 Aug. 2019
"t traffic.XX can be any values from 10 to 3600"
But in example
traffic.XX = [2500 60 0 10] %CORRECT and Want
you have a 0 element which the comment says is "%CORRECT and Want"???

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 25 Aug. 2019
Bearbeitet: dpb am 25 Aug. 2019
function x=trafficX(RP,LO)
% returns random count of X within upper limit RP but evenly divisible factor with lower limit LO
% LO is 10 by default
if nargin<2, LO=10; end % set default value if not provided explicitly
xx=[LO:10:RP]; % candidate values allowable
ix=find(fix(RP./xx)==RP./xx); % those evenly divisible
x=xx(randperm(numel(ix),1)); % return random selection from allowable subset
end
Use the above as
...
traffic.R_P = randi([1,36],TDT_Total_ID,1)*100;
traffic.XX=arrayfun(@trafficX(traffic.R_P));
...

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by