Answer to Question #209021 in MatLAB for thesynonipys

Question #209021

You need to take a matrix of any size, doubles all the elements that are positive and add 10 to all

the elements that are zero or negative. Then you need to add all the elements of the array and

display the results as follows, with the total value displayed to 3 decimal places:

The sum of the matrix elements is 75.322

i. Write a function called matrixfx

Use the input matrix xin[n,m] sent by the script,

 Initialise your out variable xout [n,m]

 to perform the above calculations on the positive and negative numbers, depending on

the values of each element of the matrix – Tip use one logical array mask to build the

matrix xout [n,m]. No loops!

ii. Write a script called matrixAdd

 Use the function matrixfx to manipulate the test data as indicated above (y).

 Initialise the necessary variables to use nested loops to add up the elements of the matrix

y.

 Your display should be formatted to display the final total as indicated above.


1
Expert's answer
2021-06-21T04:41:33-0400
function matrixA.m

function [a]= matrixA()
m=input('enter number of rows\n');
n=input('enter number of columns\n');
a=zeros(m,n);   %initializing array elements with zeros
for i=1:m
    for j=1:n
        a(i,j)=input('enter element');  %inputing elements of matrix
    end
end
disp('inputed matrix is');
disp(a)
end

function matrixfx.m

function [ xout ] = matrixfx()
xin=matrixA();  %initializing matrix elements 
xout=xin;       %copying xin to xout
x=xout>0;       %logical array mask for positive numbers
xout(x)=xout(x)*2;
x=xout<=0;      %logical array mask for negative numbers
xout(x)=xout(x)+10;
disp('matrix after performing matrixfx function')
disp(xout)

end



matrixAdd.m

y=0;
x=matrixfx();   %getting transformed matrix from matrixfx
[m,n]=size(x);
for i=1:m
    for j=i:n
        y=y+x(i,j);     %adding each and every element in matrix
    end
end
fprintf('sum of matrix elements is %.3f\n',y);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS