How can I keep just the arrays with 2 is before 3 after random swap 2 numbers.
Ältere Kommentare anzeigen
with: s=[1,2,3,4,5];
2 Kommentare
madhan ravi
am 17 Apr. 2019
Show how your output should look like.
Hang Vu
am 17 Apr. 2019
Akzeptierte Antwort
Weitere Antworten (1)
Raj
am 17 Apr. 2019
Try this:
s=[1,2,3,4,5]
%First random swap
x=randi([1,4],1,1);
if s(x)~=2
s([x x+1])= s([x+1 x]);
else
s([x x-1])=s([x-1 x]);
end
disp('After first random swap s=')
disp(s);
%second random swap
y=randi([1,4],1,1);
if s(y)~=2
s([y y+1])= s([y+1 y]);
else
%Do nothing
end
disp('After second random swap s=')
disp(s);
There may be better and optimized way of doing this also but this also works!
1 Kommentar
Hang Vu
am 18 Apr. 2019
Kategorien
Mehr zu Univariate Discrete Distributions finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!