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

madhan ravi
madhan ravi 2020년 7월 22일
A short example?
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일

0 개 추천

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

KK14
KK14 2020년 7월 22일
This would give me just 1 cell,but I need 4
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일

0 개 추천

A = zeros(100,100);
something = 1;
for c = 1:100
r = randi(97) + (0:3);
A(r,c) = something;
end

댓글 수: 3

KK14
KK14 2020년 7월 22일
In my case 97 wouldn't work.I do not always have 100 rows(question was generally asked).It would throw an error if I put randi (A(end))+(0:3) (A here is my array,say).
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.

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

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2020년 7월 22일

편집:

2020년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by