Objective Use While loop to allow program to cycle For Loop
Objective:
Use While loop to allow program to cycle. For Loop to examine vectors and arrays (Matrices). Need to find Sum and average as well as Max and Min values. See requirements below for more specifics. Input and Output to communicate with User.
Requirements:
1. The Program should loop until the user chooses to exit. Use the accompanying DIalog_Fill_Matrix.m program to learn how to enter data into a vector M(1,C) and matrix M=(R,C). A vector is one dimensional arrays and matrix are 2 dimensional arrays.
2. If a vector is input, calculate the sum of the numbers, the average (mean), the Max value and Min value. Display answers then repeat program. Note Row =1 to define a vector.
3. If a matrix is input, calculate the sum of each column and average of each column and display the sum as a vector and the average as a vector. Then find the Max value and Min value in the entire array and display them. Then repeat program. Note Row (R) >1 and Column (C) >1.
NOTE: for Sum, Average, Min, and Max DO NOT USE the functions by these names that are in MATLAB. I want to use a for loops to examine each value in the vector or array and then do the calculations.
NOTE: ALSO for Min and Max you cannot use numbers like 0 or 1000 when comparing elements to find Min and Max. What if you had negative numbers or numbers outside that range. HINT: You can you use a vector or array element instead of a preset number, if so, which one would you choose??
Incorporate the following code
%DIALOG BOX 1 CODE
%Set up for dialog box to enter matrix dimension
prompt = {\'Enter # Rows \' \'Enter # Columns\'};
dlg_title = \'Input\';
num_lines = 1;
defaultans = {\'3\' \'1\'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
%END DIALOG BOX 1\'
N = str2double(answer)
R = N(1);
C = N(2);
M =zeros(R,C);
for i=1:R
for j=1:C
%DIALOG BOX 2 CODE
%Set up dialog box to enter one row of array.
prompt = {\'Enter Element \'};
dlg_title = \'Input Elements\';
num_lines = 1;
defaultans = {\'3\'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
%END DIALOG BOX 2
%Convert string/character input to numbers
M(i,j)=str2double(answer);
end
end
M
for m=1:C
M(m)
end
Solution
Answer:
%DIALOG BOX 1 CODE
inputChoice = 1;
while(inputChoice~=3)
prompt = {\'Enter 1 - Vector functions 2 - Matrix functions 3 - Quit\'};
dlg_title = \'Input\';
num_lines = 1;
defaultans = {\'1\'};
inputChoice = str2double(inputdlg(prompt,dlg_title, num_lines,defaultans));
if inputChoice(1) == 1
[sumofVect, avgofVect, maxOfVect, minOfVect] = processVector()
elseif inputChoice(1) == 2
[sumofMatCols, avgofMatCols, maxOfMat, minOfMat] = processMatrix()
else
break;
end
end
function [sumofVect, avgofVect, maxOfVect, minOfVect] = processVector()
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
prompt = {\'Enter size of vector\'};
dlg_title = \'Input\';
num_lines = 1;
defaultans = {\'3\'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
N = str2double(answer)
R = N(1);
M = zeros(1, R);
prompt = {\'Enter row of matrix separated by space: \'};
dlg_title = \'Input\';
for i=1:R
inValue = inputdlg(prompt,dlg_title,num_lines);
M(1,i) = str2double(inValue);
end
sumofVect = 0;
avgofVect = 0;
maxOfVect = M(1,1);
minOfVect= M(1,1);
% averageValue = 0;
for j=1:R
sumofVect = sumofVect + M(i);
if (maxOfVect < M(1,i))
maxOfVect = M(1,i);
end
if (minOfVect > M(1,i))
minOfVect = M(1,i);
end
end
avgofVect = sumofVect ./ R;
end
function [sumofMatCols, avgofMatCols, maxOfMat, minOfMat] = processMatrix( )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
prompt = {\'Enter # Rows \' \'Enter # Columns\'};
dlg_title = \'Input\';
num_lines = 1;
defaultans = {\'3\' \'1\'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
N = str2double(answer)
R = N(1);
C = N(2);
M = zeros(R,C);
prompt = {\'Enter row of matrix separated by space: \'};
dlg_title = \'Input Elements\';
for i=1:R
for j=1:C
%DIALOG BOX 2 CODE
%Set up dialog box to enter one row of array.
prompt = {\'Enter Element \'};
dlg_title = \'Input Elements\';
num_lines = 1;
defaultans = {\'3\'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
%END DIALOG BOX 2
%Convert string/character input to numbers
M(i,j)=str2double(answer);
end
end
sumofMatCols = zeros(C,1);
avgofMatCols = zeros(C,1);
sumValue = 0;
maxOfMat = M(1,1);
minOfMat= M(1,1);
for i=1:C
sumValue = 0;
for j=1:R
sumValue = sumValue + M(j,i)
if (maxOfMat < M(j, i))
maxOfMat = M(j, i);
end
if (minOfMat > M(j, i))
minOfMat = M(j, i);
end
end
sumofMatCols(1,i) = sumValue;
avgofMatCols(1,i) = sumValue ./ C;
end
sumofMatCols(C,1) = sumofMatCols;
avgofMatCols = avgofMatCols;
end



