How to got about solving this loop question

1 view (last 30 days)
Strauss
Strauss on 26 Sep 2014
Commented: Adam on 26 Sep 2014
∗ We would like to generate a (5 × 5) square matrix filled with random integers between 1 and 10 and with a particular number of 1’s in the top-left to bottom-right diagonal. Write a script that asks the user to input a number of 1’s he would like to have in this diagonal (checking that the value is not bigger than 5) and then using while and for loops, the script should generate a (5×5) matrix filled with random values in [1, 10] and check if this matrix is valid. When a solution matrix has been found, print it as well as the number of tries that were required to find it. For example, running this script might result in this output:
>> DiagonalMatrix
Enter the number of 1s you want in the diagonal: 4
1 6 4 4 7
3 10 8 6 5
4 7 1 5 10
8 3 1 1 2
6 9 3 7 1
844 matrices were generated to find a good one
Not sure how to start the question. Pls help? Thanks!
  5 Comments
Strauss
Strauss on 26 Sep 2014
Hi the question wants the user to input a certain number of 1s along the diagonal. And the number of 1s must be an integer between 0 to 5. And the 1s appear randomly along the diagonals. For instance, it can appear in (1,1) and (3,3) when input is 2. However for another attempt, it can appear in (2,2) and (5,5) and so on.
Anyway this is my attempt:
x=input('Enter the number of 1s you want in the diagonal: ');
if mod(x,1)==0 && x>0 && x<=5
M=randi(10,5,5);
for n=1:x
i(n)=randi(5)
while i(n+1)~=i(n)
M(i(n),i(n))= 1
end
end
else
disp('You have entered an invalid response')
end
But it goes into infinite loop, maybe while loop is wrong?
Adam
Adam on 26 Sep 2014
I still don't understand why you need to generate 844 matrices to get a good one instead of just defining it straight away, but I guess that doesn't matter!.
I added a comment about your infinite loop below.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 26 Sep 2014
I'd start by looking up help and examples for rand() to get random numbers, inputdlg() to ask for user input, randperm() to get a specified number of locations along the diagonal for the 1's, and fprintf() to display results. Here's a snippet to get you started:
% Ask user for a number.
defaultValue = 5;
titleBar = 'Enter a value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
  1 Comment
Strauss
Strauss on 26 Sep 2014
Edited: Strauss on 26 Sep 2014
Hi,
I use the checking integer by the following commands:
x=input('Enter the number of 1s you want in the diagonal: ');
if mod(x,1)==0 && x>0 && x<=5
% checking integer and for x between 0 to 5
M=randi(10,5,5);
%generate random matrix 5,5 required size for question

Sign in to comment.


Star Strider
Star Strider on 26 Sep 2014
The randi and diag functions are your friends here.
Note that you can also use randi to generate random subscripts if you want it to. (That would work in assigning the 1 values randomly on the diagonal, for instance.)
  5 Comments
Star Strider
Star Strider on 26 Sep 2014
Edited: Star Strider on 26 Sep 2014
I don’t understand the reason for the mod call. I’m also having problems understanding your code.
This is my approach, since you have to test for the number of ones on the diagonal and not simply set them. If this violates the conditions of the problem and so would not be a valid solution, I need to know:
DD1 = 3; % Desired # of Ones On Diagonal
k = 0; % Initialise Counter
DX = false; % DX == false -> Criterion Not Met
while ~DX
k = k+1; % Increment Counter
M = randi(10, 5, 5); % Create Matrix
DM = diag(M); % Get Diagonal
DM1 = length(find(DM == 1)); % Find # of Ones on Diagonal
if DM1 == DD1 % Check If Criterion Reached
R = M; % If So, Output Result As ‘R’
DX = true; % Criterion Met
end
end
fprintf(1,'\n\tMatrix = [%2d %2d %2d %2d %2d\n\t\t\t %2d %2d %2d %2d %2d\n\t\t\t %2d %2d %2d %2d %2d\n\t\t\t %2d %2d %2d %2d %2d\n\t\t\t %2d %2d %2d %2d %2d]\n\n', R')
fprintf(1,'\n\tRequired %d Iterations\n\n',k)
There might be better and more efficient approaches, but this seems to work.

Sign in to comment.


Thorsten
Thorsten on 26 Sep 2014
Useful function are
help rand
diag
find
numel
  1 Comment
Strauss
Strauss on 26 Sep 2014
This is my attempt thus far, but it goes into infinite loop
x=input('Enter the number of 1s you want in the diagonal: ');
if mod(x,1)==0 && x>0 && x<=5
M=randi(10,5,5);
for n=1:x
i(n)=randi(5)
while i(n+1)~=i(n)
M(i(n),i(n))= 1
end
end
else
disp('You have entered an invalid response')
end

Sign in to comment.

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!