Asked by Mark Grano
on 28 Sep 2012

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

*No products are associated with this question.*

Answer by Image Analyst
on 28 Sep 2012

Edited by Image Analyst
on 28 Sep 2012

Accepted answer

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.

Mark Grano
on 28 Sep 2012

Answer by Matt Fig
on 28 Sep 2012

Edited by Matt Fig
on 28 Sep 2012

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

Show 9 older comments

Mark Grano
on 28 Sep 2012

Mark Grano
on 28 Sep 2012

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 1 Comment

## Azzi Abdelmalek (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/49295#comment_101789

it's not clear