How can I change a random element in a row of matrix?

1 次查看(过去 30 天)
I want to change a random element in a row of matrix. example
A = ones(1,84)
I wish to change 6-8 column component to the randomly 2.
However, 2 of the 6-8 columns will be get out only once.
example A = [1,1,1,1,1,1,1,1,1,1,1,1,1...] ->change A = [1,1,1,1,1,2,1,1,1,1,1,1,2,1...]
I wrote the following code.
for q=6:6:84
if rand>0.5
A(1,q) =2;
end
end
for w=7:7:84
if rand>0.6
A(1,w)=2;
end
end
for e=8:8:84
if rand>0.7
A(1,w)=2;
end
end
But I did not produce the results you want.]
Ask for advice. Plz....
  3 个评论
Ji-hwan Hwang
Ji-hwan Hwang 2015-11-29
I want A matrix 6~8(multiple) column which change 1->2 .
A = ones(1,84);
column 6~8 is 1
A(1,6)=1,A(1,7)=1,A(1,8)=1 A(1,12)=1,A(1,14)=1, A(1,18)=1 ....
6 among 8 column select change 1->2
A(1,6)=2,A(1,7)=1,A(1,8)=1 A(1,12)=1,A(1,14)=2, A(1,18)=1 ....
understand?
Walter Roberson
Walter Roberson 2015-11-30
Which columns are eligible to be changed?
Is it correct that out of all 84 columns, that exactly 6, 7, or 8 of them to be changed? So sum(A==2) would be 6, 7, or 8?
Or are you working in groups of columns, and out of each group of 8 columns, a certain number of them are to be changed randomly?

请先登录,再进行评论。

回答(0 个)

类别

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