Change one element in a row at a time with an uniform probability.

조회 수: 4 (최근 30일)
Neje
Neje 2018년 3월 31일
답변: BhaTTa 2024년 10월 14일
Hello! I want to change one element in a row of my matrix at a time with uniform probability. Currenty my matrix of [50*50] chooses one element at a time from a set of [0, 0.1, 0. 0.2, 0.3, 0.4] with an uniform probability. With the following code. But in this more than one element changes at a time which does not satisfy the above condition. How to satisfy changing one element at a time with an uniform probablility?
% code
value_set = [range];
numvalues = 50;
x_star = value_set(randi(numel(value_set), 1, numvalues));

답변 (1개)

BhaTTa
BhaTTa 2024년 10월 14일
Hey @Neje, i assume that in the given matrix you have to randomly chose a cell and replace that with a single element from the set of [0,0.1,0.2,0.3,0,4] with uniform probability
Please refer to the below code for the implementation.
value_set = [0, 0.1, 0.2, 0.3, 0.4];
matrix = zeros(50, 50);
num_updates = 100;
for i = 1:num_updates
row = randi(50);
col = randi(50);
new_value = value_set(randi(numel(value_set)));
% Update the selected element in the matrix
matrix(row, col) = new_value;
disp(matrix); % display each time so that we can see that only one cell is changing in each loop.
end
disp(matrix);
Hope it helps.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by