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

3 次查看(过去 30 天)
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