Define the following matrix: X = |4 e 3 7 8 6 1 3pi 9] You will calculate a matr
ID: 3777500 • Letter: D
Question
Define the following matrix: X = |4 e 3 7 8 6 1 3pi 9] You will calculate a matrix Y based on the components of X as follows: Y(i, j) = [Squareroot X (i, j) + 2]^2 If the calculated values of Y(i, J) is less than 10 or greater than 20, replace that value with zero. How many elements of Y are zero? What is the sum of the third column of Y? Use the code to calculate the number of nonzero elements in the matrix Z automatically, using the find and length functions. Z is defined as (18 times 18 matrix): Z = [Y Y Y Y Y Y]* [Y Y Y Y Y Y]Explanation / Answer
%defining X array
X = [4 exp(1) 3;
7 8 6;
1 3*pi 9];
%defining Y array
Y = (sqrt(X) + 2).^2;
%intializing counter for zero elements and initialising sum
numzero = 0;
sumthird = 0;
%for loop to iterate through the values
for i = 1:size(Y, 1)
for j = 1:size(Y, 2)
%if the value is less than 10 or greater than 20, make it 0
if Y(i,j)<10 || Y(i,j)>20
Y(i,j) = 0;
end
%if the value is 0, increment the counter
if Y(i,j) == 0
numzero = numzero + 1;
end
%add the elements in the third column
if j == 3
sumthird = sumthird + Y(i,j);
end
end
end
%displaying values
disp(['The number of zero elements in Y: ', num2str(numzero)]);
disp(['The sum of elements in the third column in Y: ', num2str(sumthird)]);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.