select random numbers from a group

3 次查看(过去 30 天)
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 个评论
Sriram Tadavarty
Sriram Tadavarty 2020-7-30
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?

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2020-7-30
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 个评论
mukesh meharda
mukesh meharda 2020-7-31
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 2020-8-3
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by