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
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
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.