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

write it is matlab language Write a function called small_elements that takes as

ID: 3838100 • Letter: W

Question

write it is matlab language

Write a function called small_elements that takes as input an array named X that is a matrix or a vector. The function identifies those elements of X that are smaller than the product of their two indexes. For example, if the element X(2,3) is 5, then that element would be identified because 5 is smaller than 2 * 3. The output of the function gives the indexes of such elements found in column-major order. It is a matrix with exactly two columns. The first column contains the row indexes, while the second column contains the corresponding column indexes. For example, the statement indexes = small_elements [1 1; 0 4; 6 5], will make indexes equal to [2 1; 1 2; 3 2]. If no such element exists, the function returns an empty array.

Explanation / Answer

function val=small_elements(mat)
[row,col]=size(mat);
l=1;
valr=zeros(1,numel(mat));
valc=zeros(1,numel(mat));
for i=1:col
for j=1:row
if i*j>mat(j,i)
valr(l)=j;
valc(l)=i;
l=l+1;
end
end
end
if(any(valr)==0)
val=[];
else
val=[valr',valc'];
val(val==0)=[];
val=reshape(val,[],2);
end