Answer to Question #196550 in MatLAB for Usama Hafeez

Question #196550

Write the function [elems, mns] = nonzero(A) that takes as the input argument a matrix A and

returns all nonzero elements of A in the column vector ‘elems’. The output parameter ‘mns’

holds values of the means of all columns of A.


1
Expert's answer
2021-05-21T11:57:24-0400
function[elem,mns]=nonzero(A)
%iterating through the rows and columns
n=height(A) %returns number of rows in the matrix
m=width(A) %returns number of columns in the matrix
for i=1:n
for j=1:m
if A(i,j)~=0 %if the element is not zero
elem=[elem;A(i,j)]
end
end
end

mns=mean(A,1) %mean(A,1) returns a row vector containing mean of all the columns
mns=(mns)' % we perform a transpose to store the means in a column vector
end

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