Get elements of a matrix that are greater than sum of their two indices in row major order

alisha ali

I'm Writing a function called large_elements that takes 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 > (2 + 3). The output of the function gives the indexes(row and column sub) 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.

Here is an example, the statement

indexes = large_elements([1 4; 5 2; 6 0]) 

should give the output like this:

[1 2; 2 1; 3 1]

If no such element exists, the function returns an empty array.

I have came up with the following code

function indexes = large_elements(A)
    [r c] = size(A);
    ind = 1;
    for ii = 1:r
        for jj = 1:c
            if A(ii,jj) > ii + jj
                indexes(ind,:) = [ii jj];
                ind = ind + 1;
            else
                indexes = [];
            end
       end  
    end
end

But the results are not as expected. Any help would be appreciated.

Santhan Salai

One vectorised approch using bsxfun, find and ind2sub

A = randi(8,5);    %// Your matrix

%// finding sum of the indexes for all elements
indSum = bsxfun(@plus, (1:size(A,1)).', 1:size(A,2));

%// generating a mask of which elements satisfies the given condition (i.e A > indSum)
%// Transposing the mask and finding corresponding indexes
[c,r] = find(bsxfun(@gt, A, indSum).') ;

%// getting the matrix by appending row subs and col subs
out = [r,c]

Results:

Input A:

>> A

A =

 4     4     7     2     2
 1     3     4     8     3
 8     8     2     8     7
 8     3     4     5     1
 4     1     1     1     1

Output in row-major order:

out =

 1     1
 1     2
 1     3
 2     4
 3     1
 3     2
 3     4
 4     1

Note: Getting subs in row-major order is tricky here


Also here is your correct loopy approach

[r, c] = size(A);
ind = 0;
indexes = [];
for ii = 1:r
    for jj = 1:c
        if A(ii,jj) > ii + jj
            ind = ind + 1;
            indexes(ind,:) = [ii jj];              
        end
   end  
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get elements of a matrix that are greater than sum of their two indices in row major order

From Dev

Select array elements that are greater than 5% of a sum

From Dev

Get column and row names of matrix indices in a vector

From Dev

Creating an Eigen matrix from an array with row-major order

From Dev

R matrix get indices of two highest values

From Dev

Printing the sorted elements of a matrix in descending order with array indices in the fastest fashion

From Dev

Extract matrix elements using a vector of column indices per row

From Dev

sum if greater than in r

From Java

Confusion between C++ and OpenGL matrix order (row-major vs column-major)

From Dev

Summing the rows of a matrix (stored in either row-major or column-major order) in CUDA

From Dev

Confusion between C++ and OpenGL matrix order (row-major vs column-major)

From Java

pandas - how to count number of elements in a row greater than zero?

From Dev

jquery-validate: validate if the sum of two fields is greater than X

From Dev

jquery-validate: validate if the sum of two fields is greater than X

From Dev

Querying rows that have data greater than the sum of two columns

From Dev

Check if at least two elements in an array are greater than zero - JavaScript/Typescript

From Dev

In MATLAB, extract row indices from a larger matrix which contains all row elements of a 4*3 matrix

From Dev

In MATLAB, extract row indices from a larger matrix which contains all row elements of a 4*3 matrix

From Dev

Get least sum of a combination of matrix elements

From Dev

Get least sum of a combination of matrix elements

From Dev

Sum of Major Diagonal Matrix JAVA

From Java

How to get indices of lil_matrix elements in a for loop?

From Dev

Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE?

From Dev

Python row major to column major order vector

From Dev

How to sort a matrix in the ascending order by the sum of its row in Python?

From Dev

Add negative elements in matrix to whole row under the condition that all elements except diagonal are greater 0

From Dev

Row Major Order Address calcuatlion

From Dev

Row Major Order Address calcuatlion

From Dev

Getting indices of sorted elements of matrix

Related Related

  1. 1

    Get elements of a matrix that are greater than sum of their two indices in row major order

  2. 2

    Select array elements that are greater than 5% of a sum

  3. 3

    Get column and row names of matrix indices in a vector

  4. 4

    Creating an Eigen matrix from an array with row-major order

  5. 5

    R matrix get indices of two highest values

  6. 6

    Printing the sorted elements of a matrix in descending order with array indices in the fastest fashion

  7. 7

    Extract matrix elements using a vector of column indices per row

  8. 8

    sum if greater than in r

  9. 9

    Confusion between C++ and OpenGL matrix order (row-major vs column-major)

  10. 10

    Summing the rows of a matrix (stored in either row-major or column-major order) in CUDA

  11. 11

    Confusion between C++ and OpenGL matrix order (row-major vs column-major)

  12. 12

    pandas - how to count number of elements in a row greater than zero?

  13. 13

    jquery-validate: validate if the sum of two fields is greater than X

  14. 14

    jquery-validate: validate if the sum of two fields is greater than X

  15. 15

    Querying rows that have data greater than the sum of two columns

  16. 16

    Check if at least two elements in an array are greater than zero - JavaScript/Typescript

  17. 17

    In MATLAB, extract row indices from a larger matrix which contains all row elements of a 4*3 matrix

  18. 18

    In MATLAB, extract row indices from a larger matrix which contains all row elements of a 4*3 matrix

  19. 19

    Get least sum of a combination of matrix elements

  20. 20

    Get least sum of a combination of matrix elements

  21. 21

    Sum of Major Diagonal Matrix JAVA

  22. 22

    How to get indices of lil_matrix elements in a for loop?

  23. 23

    Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE?

  24. 24

    Python row major to column major order vector

  25. 25

    How to sort a matrix in the ascending order by the sum of its row in Python?

  26. 26

    Add negative elements in matrix to whole row under the condition that all elements except diagonal are greater 0

  27. 27

    Row Major Order Address calcuatlion

  28. 28

    Row Major Order Address calcuatlion

  29. 29

    Getting indices of sorted elements of matrix

HotTag

Archive