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.
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
Comments
Leave a comment