select random numbers from a group

3 ビュー (過去 30 日間)
mukesh meharda
mukesh meharda 2020 年 7 月 30 日
コメント済み: the cyclist 2020 年 8 月 3 日
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 Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by