copy matrix and random it's value within certain range
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
i have recently post Copy certain number from one matrix to another. Similar to this, how can i change the value of a matrix random it within certain range? The result of matrix C and matrix D is based on matrix B value. For example
A B C D % C D
------------- %
1 1 1 1 %copy 1 to C copy 1 to D
2 1 1 1 %copy 1 to C copy 1 to D
3 3 3 3 %random 1 or 3.get 3 random 1 or 3.get 3
4 3 3 1 %random 1 or 3.get 3 random 1 or 3.get 1
5 3 1 1 %random 1 or 3.get 1 follow previous value. 1
6 5 5 5 %copy 5 random 1 or 5. get 5
7 5 5 1 %copy 5 random 1 or 5. get 1
8 4 4 1 %random 1 or 4.get 4 random 1 or 4. get 1
9 4 1 1 %random 1 or 4.get 1 follow previous value. 1
10 4 1 1 %follow previous value.1 follow previous value. 1
11 1 1 1 %copy 1 to C copy 1 to D
12 9 9 9 %copy 9 to C random 1 or 9. get 9
13 9 9 1 %copy 9 to C random 1 or 9. get 1
14 9 9 1 %copy 9 to C follow previous value. 1
15 3 3 3 %random 1 or 3.get 3 random 1 or 3. get 3
16 3 3 1 %random 1 or 3.get 3 random 1 or 3.get 1
17 3 1 1 %random 1 or 3.get 1 follow previous value. 1
18 8 8 8 %copy 8 random 1 or 8. get 8
19 8 8 8 %copy 8 random 1 or 8. get 8
20 8 8 1 %copy 8 random 1 or 8. get 1
21 0 0 1 %copy 0 random 1 or 0. get 0
22 0 0 1 %copy 0 follow previous value
Matrix A and B i already have. Matrix D is slightly have more 1 compare to C.
For matrix C : For matrix D
0 = 0, 0 = 1,
1 = 1, 1 = 1,
2 = random between 1 or 2, 2 = random between 1 or 2,
3 = random between 1 or 3, ...
4 = random between 1 or 4, ...
5=5, 6=6, 7=7, 8=8, 9=9 9 = random between 1 or 9,
Whenever its random and get, the following value is also set to 1.
1 1
3 3 %random
3 1 %random and get 1
3 1 %follow previous value
1 1 %copy 1
3 3 %random again
0 commentaires
Réponses (1)
dpb
le 6 Oct 2016
Modifié(e) : dpb
le 6 Oct 2016
Bmn=2; Bmx=4; % ranges for B
ix=iswithin(B,Bmn,Bmx); % rule for positions
C(ix)=(rand(size(B))>0.5).*B(ix); % pick either B or 1, 50:50
Simply change the ranges for D
iswithin is my utility function
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
that is often quite useful in cases such as this to remove the complexity of the logical expressions to a lower level making the top-level code much cleaner.
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!