random selection of a cell

18 次查看(过去 30 天)
KK14
KK14 2020-7-22
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
  2 个评论
KK14
KK14 2020-7-22
I have a 100*100matrix. Suppose I take x as my random value & column say 'y'.So my selection among the 100 elements in the column should be (x:x+4, y)
ex: if x = 7, y =4
selection = (7:10, 4)

请先登录,再进行评论。

采纳的回答

KSSV
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
  3 个评论
KSSV
KSSV 2020-7-22
编辑:KSSV 2020-7-22
You said you want continuously right? Then simply taken next four from them.
% loop for each column
for i = 1:100
% pick element randomly
p = randperm(100,1) ;
if p > 96
p = 96 ;
end
idx = p:p+4 ;
end
KK14
KK14 2020-7-22
Thankyou for the answer.Idea works just fine.

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
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
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.
KK14
KK14 2020-7-22
Thankyou for the answer.Had to modify, but the idea helped.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by