Write a MATLAB program in a script file that asks the user to input a matrix of
ID: 2080613 • Letter: W
Question
Write a MATLAB program in a script file that asks the user to input a matrix of any arbitrary size. Then the program must use nested for loops to examine each element of the matrix. If the element is positive its value is doubled. If the element is negative its value is tripled. The value of zero can be done in each way (it is not altered). The program displays the matrix that was entered and the modified matrix as well. Use the program with the following matrices: 0 13 -8 -6 -2 -8 -1 -9 8 1 4 3 -7 10 1 -12 -9 -9 -7 -15 0 -2 8 -2 -15 7 14 -14 -17 -13 -2 2 3 6 -18 -14 -8 9 -18 -9Explanation / Answer
Execute the following MATLAB program:
%%Program
clc;
close all;
clear all;
%% Ask the user to input a matrix
a = 'Enter any Matrix of any arbitrary size: ';
%% Read the input as b
b = input(a);
%% Find the size of the matrix
[rows cols]=size(b);
%% Initialize a null Matrix to store the results for every loop execution
c=zeros(rows,cols);%%rows and columns
%% loop
for i=1:rows
for j=1:cols
if b(i,j)>=0 %%for positive values
c(i,j)=2*b(i,j);%%double the value
else %%for negative values
c(i,j)=3*b(i,j);%%triple the value
end
end
end
%% display the results
disp('The entered matrix is :')
disp(b)
disp('The modified matrix is :')
disp(c)
(a)
Observe the following output in the command window and enter the matrix as shown below.
Enter any Matrix of any arbitrary size: [0 13 -8 -6; -2 -8 -1 -9; 8 1 4 3]
Press Enter key and obtain the following results:
The entered matrix is :
0 13 -8 -6
-2 -8 -1 -9
8 1 4 3
The modified matrix is :
0 26 -24 -18
-6 -24 -3 -27
16 2 8 6
(b)
Execute the program agin and observe the following output in the command window and enter the matrix as shown below.
Enter any Matrix of any arbitrary size: [-7 10 1 -12; -9 -9 -7 -15; 0 -2 8 -2; -15 7 14 -14]
Press Enter key and obtain the following results:
The entered matrix is :
-7 10 1 -12
-9 -9 -7 -15
0 -2 8 -2
-15 7 14 -14
The modified matrix is :
-21 20 2 -36
-27 -27 -21 -45
0 -6 16 -6
-45 14 28 -42
(c)
Execute the program agin and observe the following output in the command window and enter the matrix as shown below.
Enter any Matrix of any arbitrary size: [-17 -13 -2 2 3 6;-18 -14 -8 9 -18 -9]
Press Enter key and obtain the following results:
The entered matrix is :
-17 -13 -2 2 3 6
-18 -14 -8 9 -18 -9
The modified matrix is :
-51 -39 -6 4 6 12
-54 -42 -24 18 -54 -27
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.