Search Program on this blog

Wednesday 19 August 2015

Write a function called large_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 greater than the sum of their two indexes. For example, if the element X(2,3) is 6, then that element would be identified because 6 is greater than 2 + 3. The output of the function gives the indexes of such elements found in row-­‐ 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 = large_elements([1 4; 5 2; 6 0], will make indexes equal to [1 2; 2 1; 3 1]. If no such element exists, the function returns an empty array.

function indexes = large_elements(x)
%Input x is a matrix or vector
%Output indexes is a matrix with two column
%Example: x = [1 4;5 2;6 0]
%         indexes = large_elements(x)
%         indexes = [1 2;2 1; 3 1]
indexes=[];
[m,n]=size(x);  %dimensions of x
for i=1:m
    for j=1:n
        if x(i,j)>(i+j)   
            indexes = [indexes; i j];  %indexes with value>=sum of indexes
        end
    end
end

No comments:

Post a Comment