Replacing parts of a matrix randomly
Show older comments
Hello I am trying to creat a programs that aska the user for an input from 1-15 and then randomly replaces that amount in a matrix. so for the matrix:
1 ABC DEF
2 ABC DEF
3 ABC DEF
4 ABC DEF
5 ABC DEF
6 ABC DEF
7 ABC DEF
it will randomly replace say input(5) letters to the letter x. the only problem i am having is that i don't want the first, second, and sixth columns to be selected.
clear;clc;
m=['1 ' 'ABC DEF'; '2 ' 'ABC DEF'; '3 ' 'ABC DEF'; '4 ' 'ABC DEF'; '5 ' 'ABC DEF'; '6 ' 'ABC DEF'; '7 ' 'ABC DEF';];
disp(m);
num= input('Enter an integer from 1 to 15: ');
f='y';
while (f=='y' || f=='Y')
if (num>=1 && num<=15 && rem(num,1)==0)
a=randperm(7,1);
b=randperm(9,1);
b~=1 && b~=2 && b~=6 && b~=0 && a~=0;
m(a,b)='x';
num=(num-1);
if (num==0);
break;
end
else disp(' ');
disp( 'The number you entered was not in the specified range. '); num=input('Please enter an integer between 1 and 15: ');
end
end
disp(m);
f=input('Enter y/Y to continue. All else terminates: ', 's');
This is the code i have come up with. All i need help with is to fix it to where it will not use those columns, to where it cannot duplicate the same result and replace the same letter twice, and for some reason i am having problems with the question at the end to continue or not. It seems to not work very well. I know this is a lot but if someone could help me iron this program out i would greatly appreciate it.
Mark Grano
1 Comment
Azzi Abdelmalek
on 28 Sep 2012
it's not clear
Accepted Answer
More Answers (1)
Before your loop define:
X = [3 4 5 7 8 9];
Then when to pick your b, simply do this:
b = X(randi(6,1,1));
Thus no matter what randi returns, it will index into X to only get one of the allowed columns as defined in X.
.
.
.
I will also show how I would do what I think you are wanting to do:
function [] = replace_nums()
% Replace elements of an array randomly.
m=['1 ' 'ABC DEF'; '2 ' 'ABC DEF'; '3 ' 'ABC DEF';...
'4 ' 'ABC DEF'; '5 ' 'ABC DEF'; '6 ' 'ABC DEF';...
'7 ' 'ABC DEF';];
disp(m);
X = [3 4 5 7 8 9];
f = 'y';
while isequal('y',lower(f))
num = 0;
cnt = 1;
while num<1 || num>15
num = input('Enter an integer from 1 to 15: ');
cnt = cnt + 1;
if cnt>4,disp('Bored yet?'),return,end
end
% Loop could be replaced by:
% a = randi(7,1,num);
% b = X(randi(6,1,num));
% m(sub2ind([7,9],a,b)) = 'x';
for ii = 1:num
a = randi(7,1,1);
b = X(randi(6,1,1));
m(a,b) = 'x';
end
disp(m)
f = input('Enter y to continue. All else quits: ', 's');
end
12 Comments
Mark Grano
on 28 Sep 2012
Matt Fig
on 28 Sep 2012
No problem. I forgot you also said the first column. I fixed the above code to accommodate for this...
Mark Grano
on 28 Sep 2012
Well it is entirely possible to draw the same row & column number during a run of the loop. So that would explain why you might get less than num replacements during any single run of the loop. When drawing randomly there is always a chance to get repeats, thus replacing an 'x' with another 'x'. This could be eliminated by more careful coding.
But I would say it is impossible that you got 12 when you asked for 9 because the FOR loop only goes to num! Unless MATLAB is completely broken, a FOR loop will not go past it's limit. I would venture a guess that you forgot that the array doesn't reset each time through... So you might have had 3 x's from the time before then got your 9 on top of it, making 12 x's.
Mark Grano
on 28 Sep 2012
Mark Grano
on 28 Sep 2012
Matt Fig
on 28 Sep 2012
I used a function, so there is no need to put clear,clc in it because it has it's own workspace. Now, are you saying that each time you ask the user if he wants to continue you want to start with a fresh character array m?
Mark Grano
on 28 Sep 2012
O.k., as the very first line of the outer WHILE loop (before the line: num = 0;), put this:
mc = m;
Then everywhere inside the outer WHILE loop replace m with mc. That will give a fresh start every time.
As far as the integer checking, just add another check to the inner WHILE loop conditional:
|| floor(num)~=num
To ensure there are exactly num replacements each time, I would replace the FOR loop with a WHILE loop that increments a counter up to num and replaces mc(a,b) only if mc(a,b)~='x'.
Good luck!
Mark Grano
on 28 Sep 2012
Matt Fig
on 28 Sep 2012
Yes, I addressed it in my last comment. "To ensure there are exactly num replacements each time..."
Mark Grano
on 28 Sep 2012
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!