Replacing parts of a matrix randomly

1 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2012-9-28
编辑:Image Analyst 2012-9-28
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.
  1 个评论
Mark Grano
Mark Grano 2012-9-28
Thank you for taking the time to help me. The reason why i included 1to 15 was because that determines how many letters to replace. If i enter in 5, then 5 letters should be replaced, not just one. Everything else has fixed the bugs i had. Thank you for your time once again.

请先登录,再进行评论。

更多回答(1 个)

Matt Fig
Matt Fig 2012-9-28
编辑:Matt Fig 2012-9-28
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 个评论
Matt Fig
Matt Fig 2012-9-28
Yes, I addressed it in my last comment. "To ensure there are exactly num replacements each time..."
Mark Grano
Mark Grano 2012-9-28
oh wow, my bad, completely missed that. Thank you for all your time once again, you really helped me out.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by