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

I need the vectorize code a) vectorize your code and thus the function should ha

ID: 3719204 • Letter: I

Question

I need the vectorize code

a) vectorize your code and thus the function should have a single line of code besides the function heading and
end lines and comment line.

This is the none vectorized code:

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

P'roblem 2) Given a matrix M (any matrix, any size Write a MATLAB 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

Explanation / Answer

-------------------------sumneg.m---------------------

function [sumnegmax] = sumneg(M , max_val)

    sumnegmax = 0;

    % get the dimensions of M

    [r , c] = size(M);

   

    for i = 1 : r

       

        for j = 1 : c

           

            % if current elements is greater than max

            if M(i , j) > max_val

               

                % negate the sign

                M(i , j) = -M(i, j);

               

            end

           

        end

       

    end

   

    % calculate the sum of all elements

    for i = 1 : r

       

        for j = 1 : c

           

            sumnegmax = sumnegmax + M(i , j);

           

        end

       

    end

end

-----------------------main.m---------------------

M=[3 6 9; 12 15 18; 21 24 27];

max_val = 14;

sumnegmax = sumneg(M , max_val);

M

max_val

sumnegmax

Sample Output

M =

3 6 9
12 15 18
21 24 27


max_val =

14


sumnegmax =

-75

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