How to got about solving this loop question
1 view (last 30 days)
Show older comments
∗ 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
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.
Answers (3)
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
Star Strider
on 26 Sep 2014
5 Comments
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.
See Also
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!