select random numbers from a group

조회 수: 5 (최근 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 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