Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Find numbers and location in a matrix , and put it into new matrix

Subject: Find numbers and location in a matrix , and put it into new matrix

From: HÃ¥vard

Date: 7 Dec, 2013 14:05:20

Message: 1 of 2

Hi!
I am trying to make a function which is to find a certain number, or numbers above a threshold number, in a given matrix of unknown size. Preferably without any other function then if,else,while,for.

function search = searchMatrix(matrix)

search = [];

[r c] = size(matrix);

for i = 1:r
    for j = 1:c
        if (matrix(i,j) >= 2000)
            
            search = matrix(i,j) && i && j;
        end
    end
end

But I would like the numbers, and the location of the numbers to be put into a new matrix "search" with each new row corresponding to these numbers and location.
E.g:
[ 2300 4 5 ; 4320 8 2 ; 2001 9 4]

Im not sure how to proceed for doing so. Any suggestions?

Subject: Find numbers and location in a matrix , and put it into new matrix

From: Sadik

Date: 8 Dec, 2013 02:16:10

Message: 2 of 2

This should do it:

search = [];

[r c] = size(matrix);

for i = 1:r
    for j = 1:c
        if matrix(i,j) >= 2000
            search = [search; matrix(i,j) i j];
        end
    end
end

% ========================
% A better one
% ========================
[row,col] = find(matrix>2000);
search = [matrix(matrix>2000) row col];
% ========================

Best.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us