Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the Non vectorized code that I have. M=[3 6 9; 12 15 18; 21 24 27]; %Mat

ID: 3719084 • Letter: T

Question

This is the Non vectorized code that I have.

M=[3 6 9; 12 15 18; 21 24 27]; %Matrix Declaration
max=18; %MaxValue
[rows, cols] = size(M); %to givem the size of the matrix in FILAS,COLUMNAS
sum_neg_max = 0; %Acumulador
%****NESTED LOOP****
for i = 1 : rows
for j = 1 : cols
if M(i, j) > max
M(i, j) = M(i, j) * -1;
end
sum_neg_max = sum_neg_max + M(i, j);

end
end
%**** Showing Results and Variables***
M
max
sum_neg_max

Problem 2) Given a matrix M (any matrix, any size) Write a MA TLAB function [sumnegmax]- sumneg(M.max) that negates (change sign) to every element strictly greater than the number max in the matrix M and sum all the resultant entries in the matrix. This number, the sum of all matrix elements where those that are greater than max are first negated, is called sumnegmax. The code should be general, that is, work for a matrix of any dimension and any value of max Example: with max 6 (remember: your code should work for any max and any matrix) Matrix M is: 5 -6 9 10 therefore sumneg--17 Step by step: negating entries strictly larger than 6 gives: 5 -6 91 Summing all entries gives -17 (Note that you can also sum/accumulate your entries as you go, making sure you first negate any entry that is larger than max before accumulation.)

Explanation / Answer

PLEASE REFER BELOW CODE

close all
clear all
clc

M=[1 2; 5 -6; 9 10] %Matrix Declaration
max = 6
[sumnegmax] = sumneg(M,max)
[sumnegmax_vect] = sumneg_vectorize(M,max)

function [sumnegmax] = sumneg(M,max)
[r,c] = size(M);
sumnegmax = 0;
for i = 1:r
for j = 1:c
if(M(i,j) > max)
M(i,j) = M(i,j) * -1;
end
sumnegmax = sumnegmax + M(i,j);
end
end
end

function [sumnegmax_vect] = sumneg_vectorize(M,max)
M(M>max) = M(M>max) * (-1); %finding element greater than max and change sign
sumnegmax_vect = sum(M(:));
end

PLEASE REFER BELOW OUTPUT


M =

1 2
5 -6
9 10


max =

6


sumnegmax =

-17


sumnegmax_vect =

-17

>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote