random selection of a cell
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have a cell(matrix) of size 100*100.I need to scan each column and choose 4 consecutive random cells( (1*1)*4) for assigning values.Can anyone please help me with this?
Thanks in advance
採用された回答
KSSV
2020 年 7 月 22 日
You got 100 columns.....you can pick any one element/ position out of it randomly using randperm.
% loop for each column
for i = 1:100
% pick element randomly
p = randperm(100,1) ;
end
その他の回答 (1 件)
Bruno Luong
2020 年 7 月 22 日
A = zeros(100,100);
something = 1;
for c = 1:100
r = randi(97) + (0:3);
A(r,c) = something;
end
3 件のコメント
Bruno Luong
2020 年 7 月 22 日
編集済み: Bruno Luong
2020 年 7 月 22 日
Replace "r = randi(97) ..." by
ncons = 4;
r = randi(size(A,1)-ncons+1)+(0:ncons-1)
You also specify 4 consecutive rows in your original question. Feel free if you want change 4 to something else.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!