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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by