choose 2 random unique element inside cell

how to choose 2 random unique element inside a cell?
index={[1,2,5,9,10,13,17,18,21],[4,5,7,12,13,15,18,20,21],[3,4,6,11,12,14,18,19,20],[8,16,20,22]};
example of result
p={[1,9],[7,21],[3,4],[16,8]}

 채택된 답변

per isakson
per isakson 2019년 2월 23일
편집: per isakson 2019년 2월 23일

0 개 추천

and try
>> cellfun( @(row) datasample( row, 2, 'replace',false ), index, 'uni',false )
ans =
1×4 cell array
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
"[...] random unique element inside a cell" does that mean without replacement?
If it means literal "unique elements" then run this is a first step to remove duplicates.
>> index = cellfun( @unique, index, 'uni',false )
ix =
1×4 cell array
{1×9 double} {1×9 double} {1×9 double} {1×4 double}
Or should the propability to pick a specific value be proportional to the number of duplicates of that value?

댓글 수: 8

NA
NA 2019년 2월 23일
편집: per isakson 2019년 2월 24일
As I want to run simulation 5 times, how can I fix datasample?
per isakson
per isakson 2019년 2월 24일
편집: per isakson 2019년 2월 24일
Problem: You have a working script that runs the simulation once and you want to run it automatically many times
The first approach you shall consider is a for-loop.
  1. Convert the script to a function
  2. Create a second function that calls the first many times
  3. Run the second function
Example
>> out = cssm( index, 5 )
out =
5×1 cell array
{1×4 cell}
{1×4 cell}
{1×4 cell}
{1×4 cell}
{1×4 cell}
>> out{3}
ans =
1×4 cell array
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
where
function out = cssm( index, n )
out = cell( n, 1 );
for jj = 1 : n
out{jj} = run_once( index );
end
end
function out = run_once( index )
out = cellfun( @(row) datasample( row, 2, 'replace',false ) ...
, index, 'uni',false );
end
BTW:
%%
index = repmat( index, 5,1 );
out = cellfun( @(row) datasample( row, 2, 'replace',false ), index, 'uni',false )
outputs
out =
5×4 cell array
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
What happened to the comments on the interpretation of "unique"?
NA
NA 2019년 2월 24일
편집: NA 2019년 2월 24일
I have this code and want to run it 5 times (amount is changing in each iteration)
for iteration=1:5
index={[1,2,5,9,10,13,17,18,21],[4,5,7,12,13,15,18,20,21],[3,4,6,11,12,14,18,19,20],[8,16,20,22]};
amount={[10,11,12,10,11,10,12,13,20],[11,15,12,13,11,10,15,13,12],[13,16,16,17,11,10,15,13,12],[4,5,6,10]}; % amount is changing in each iteration
changed_num=2;
if iteration==1
changed_mes_index=cellfun( @(m) datasample( m, changed_num, 'replace',false ), index, 'uni',false );
end
b=cellfun( @(t,m,u) t/2.*(ismember(m,u)), amount,index,changed_mes_index, 'uni',false );
result=cellfun(@(m,y) m+y, amount,b, 'UniformOutput', false);
end
per isakson
per isakson 2019년 2월 24일
편집: per isakson 2019년 2월 24일
"you mean I do something like this" No!
What happend to "I want to run simulation 5 times"?
for iteration=1:5
...
end
isn't meaningful. It does the same thing five times and overwrites result. How is amount supposed to change?
It's hard to help, since you don't try to understand my answer and move the goalposts in the comments.
And I repeat, what happened to the comments on the interpretation of "unique"?
I want to do something like this
if iteration==1
changed_mes_index=cellfun( @(m) datasample( m, changed_num, 'replace',false ), index, 'uni',false );
end
this code
index = repmat( index, 5,1 );
out = cellfun( @(row) datasample( row, 2, 'replace',false ), index, 'uni',false )
does not give what I want
NA
NA 2019년 2월 24일
편집: per isakson 2019년 2월 24일
amount is calculated by adding normrnd. so it changes in each iteration.
"amount is calculated by adding normrnd" That means that you need to be careful regarding Accuracy of Floating-Point Data
So far, I have assumed whole numbers

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jos (10584)
Jos (10584) 2019년 2월 23일

0 개 추천

If I understand you correctly, the final output should have 8 unique numbers, in four groups of two, where the numbers in each group is drawn from a cell in the cell array index. This is not a trivial problem to answer! There might situations where this is simply impossible given a cell array index (for instance index= {[1 2 3], [1 2 4], [2 3 4],... }
You might want to try a brute-force method
index={[1,2,5,9,10,13,17,18,21],[4,5,7,12,13,15,18,20,21],[3,4,6,11,12,14,18,19,20],[8,16,20,22]};
fn = @(v) sort(v(randperm(numel(v),2))) ; % function to draw 2 randomelements from v
k = 1 ;
while k < 1e5 % try a lot of times
C = cellfun(fn, index,'un',0) ;
if numel(unique([C{:}])) == 8,
break
else
C = [] ; k = k+1 ;
end
end
celldisp(C)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

NA
2019년 2월 23일

댓글:

2019년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by