MATLAB: Elements of a matrix that are greater than sum of their two indices problem

for loopshomeworkwhile loops

I am having trouble solving the following problem.
Prompt: 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 rowmajor 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.
My attempt:
function L = large_elements(x)
[r,c] = size(x);
[r,c] = find(x > bsxfun(@plus, (1:r)', 1:c));
L = sortrows([r,c]);
Any help and/or advice would be much appreciated.

Best Answer

Make this change:
L = sortrows([r(:),c(:)]);