select random numbers from a group

2 views (last 30 days)
mukesh meharda
mukesh meharda on 30 Jul 2020
Commented: the cyclist on 3 Aug 2020
I had number of EV in different time stamp in each time, and a group of their battery capacity and type of it. first randomly divide number of EVs in 2nd column as
for i=1:24
N_EV(i)= randi(15,1,1); % not more than 15
end
Now I had Cap=[2,4,6];
so for each EVs , select a random Cap from this Cap as shown in 4th column,
Then based on logic in 6th column based on Cap value define their type of EVs
  1 Comment
Sriram Tadavarty
Sriram Tadavarty on 30 Jul 2020
Whats your question here? Can you update the question with the data set, the code you tried, place where you struck or getting incorrect outputs?

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 30 Jul 2020
I think I understand at least part of your question.
Do you have the Statistics and Machine Learning Toolbox? If so, then to select 4 EV from set of Caps [2,4,6], you can do
randsample([2,4,6],4)
  2 Comments
mukesh meharda
mukesh meharda on 31 Jul 2020
thanks for your help and I solved it as below that I need......
B_Cap = [2;4;6];
for i=1:24
N_EV(i)= randi(15,1,1);
end
cell=cell(24,1);
for i=1:24
for j=1:N_EV(i)
pos(j) = randi(length(B_Cap));
cap(j) = B_Cap(pos(j)); % capacity selection for each EVs
end
cell{i,1} = cap;
pos=[];
cap=[];
end
% SoCi defining for each EVs based on their type
for i=1:24
for j=1:N_EV(i)
if cell{i,1}(j)==2
cell{i,2}(j)=0.2; %if 2W then SoCi
else if cell{i,1}(j) ==6
cell{i,2}(j)= 0.3; %if 3W then SoCi
else cell{i,2}(j)= 0.25; %if eR then SoCi
end
end
end
end
the cyclist
the cyclist on 3 Aug 2020
I'm glad you found a solution.
I strongly recommend against using cell as a variable name, since it is a MATLAB function. This is a very confusing line of code:
cell=cell(24,1);
for anyone trying to understand what you are doing, and is also prevents future use of cell() as a function.
It's better to give that variable a meaningful name.

Sign in to comment.

Categories

Find more on Random Number Generation 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!