MATLAB Answers

Naime
0

select random row inside the cell

Asked by Naime
on 5 Apr 2019
Latest activity Edited by Jos (10584)
on 5 Apr 2019
A={[1,4,6;6,5,4;1,2,4],[1,3;5,4],[1,2;1,7;6,8]};
number_of_choosen_row=1;
choosen_row=cellfun( @(m) datasample( m,number_of_choosen_row,'replace',false ), A, 'uni',false);
I want to chose random row inside a cell. but above code does not give me correct result.
for example result should be choosen_row={[3],[1],[1]}

  0 Comments

Sign in to comment.

1 Answer

Answer by Jos (10584)
on 5 Apr 2019
Edited by Jos (10584)
on 5 Apr 2019
 Accepted Answer

You want the row, or the row number to be returned?
A={[1,4,6;6,5,4;1,2,4],[1,3;5,4],[1,2;1,7;6,8]}
rownumber = cellfun(@(c) randperm(size(c, 1), 1), A)
row = cellfun(@(c) c(randperm(size(c, 1), 1), :), A, 'un', 0)

  0 Comments

Sign in to comment.