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.
m=['1 ' 'ABC DEF'; '2 ' 'ABC DEF'; '3 ' 'ABC DEF'; '4 ' 'ABC DEF'; '5 ' 'ABC DEF'; '6 ' 'ABC DEF'; '7 ' 'ABC DEF';];
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)
b~=1 && b~=2 && b~=6 && b~=0 && a~=0;
m(a,b)='x'; num=(num-1); if (num==0);
else disp(' '); disp( 'The number you entered was not in the specified range. '); num=input('Please enter an integer between 1 and 15: ');
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.
No products are associated with this question.
Try this code:
m=['1 ' 'ABC DEF'; '2 ' 'ABC DEF'; '3 ' 'ABC DEF'; '4 ' 'ABC DEF'; '5 ' 'ABC DEF'; '6 ' 'ABC DEF'; '7 ' 'ABC DEF';] while true num= input('\nEnter an integer from 1 to 15: '); if num <= 1 || num > 15 fprintf('%d is not between 1 and 15 inclusive.\n', num); continue; end fprintf('You entered %d but I do not do anything with it.\n', num); % Now get random row and column randomRow = randi(7, 1); randomColumn = randi(9, 1); if randomColumn == 1 || randomColumn == 2 || randomColumn == 6 message = sprintf('Column %d was chosen at random but I am not allowed to change that\n', randomColumn); uiwait(msgbox(message)); continue; end fprintf('Row %d, column %d changed to x:\n', randomRow, randomColumn); m(randomRow, randomColumn) = 'x' promptMessage = sprintf('Do you want to Continue or Cancel?'); titleBarCaption = 'Continue?'; button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue'); if strcmp(button, 'Cancel') break; end end
I don't know why the asking for a number less than 15 was in there. I left it in but you can take it out. I just ask the user via questdlg() if they want to continue or quit.
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';
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
Play games and win prizes!Learn more