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

Please include the code in the solution Please write a MATLAB function that find

ID: 1840623 • Letter: P

Question

Please include the code in the solution

Please write a MATLAB function that finds all positive entries in a matrix and their locations (i.e. row and column indices). Your function header should be: function [posentry, location] = find positive (A) where the input A is a matrix; the first output posentry is a vector that, contains all positive entries from A, and the second output location is a matrix with two columns, where the first column contains all the row indices and the second column contains all the column indices of those positive entries. For example, if your input matrix A = the positive entries are: Your two outputs should be posentry y = and location = Please test the Matlab code on a couple of examples.

Explanation / Answer

MATLAB code:

function [posentry,location] = findpositive(A)
x=numel(A);
[m,n]=size(A);
j=1;
for i=1:x
    if A(i)>0
        xx(j,:)=A(i);
        j=j+1;
    end
end
posentry=xx
sz=length(posentry);
i=0;
for i=1:m
    for j=1:n
        for k=1:sz
        if A(i,j)==posentry(k)
            yy(j,:)=[i,j];
        end
        end
    end
end
location=yy
end

sample output

>> findpositive([3 -5 0;-9 2 4;-2 -1 2])

posentry =

     3
     2
     4
     2


location =

     1     1
     2     2

     2     3
     3     3