Find number and location in matrix, make a new matrix with findings.

2 views (last 30 days)
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?

Answers (3)

dpb
dpb on 7 Dec 2013
Preallocate search for speed improvement in large cases, then populate the array. To cover the worst-case, it would have to be numel(matrix) in size...
srch=zeros(numel(m),1); % preallocate
Then
srch(i,:)=[m(i,j i j]; % each found element
If you keep an additional counter you can truncate unused rows using it to avoid the use of another builtin ( any, all, etc.) or another logical test.

Håvard
Håvard on 7 Dec 2013
Thank you for your answer!
As of your explanation I tried:
function search = searchMatrix(matrix)
search = zeros(numel(matrix),3);
[r, c] = size(tabell);
for i = 1:r
for j = 1:k
if (tabell(i,j) >= 2000)
search(i,:) = [tabell(i,j),i,j];
end
end
end
end
with input "searchMatrix([ 1990 2001 2002; 2134 1983 1500; 1459 2000 1400])" i received:
2002 1 3
2134 2 1
2000 3 2
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
a lot of zeros, as you predicted, but it seemed to only be able to get the last number from the row. How can I fix this?
Also, I felt maybe my main concern were not answer, so I'm gonna try with another example. If I make a function which takes in two parameters, and figure out how many times it hit a certain number between the two given parameters, as in this olympics example:
function years = summerOlympics(firstYear,lastYear)
years = [];
for i = firstYear:lastYear
if mod(i,4) == 0;
years(i) = i
end
end
end
e.g firstYear = 1999, and lastYear=2012, should give out years = [ 2000 2004 2008 2012] If I here try to do as previously, I´ll get a row of over 2000 zeros. I just want for it to take the true if-condition, and put it neatly into 1,2,3 etc vector space of years.
  1 Comment
dpb
dpb on 7 Dec 2013
A) The handling of the values is owing to not incrementing another counter for the location...
n=0;
...
if m(i,j)>setpoint,
n=n+1;
s(n,:)=[m(i,j) i j];
...
B) As for truncation, as suggested earlier, you have to truncate to the used portion. Now having n, when done thru the loop
s(n+1:end,:)=[]; % clear unused rows
And, of course, while I presume these exercises are pedagogical self-teaching tools, "the Matlab way" would be to use logical addressing and eliminate the loops entirely.

Sign in to comment.


Image Analyst
Image Analyst on 7 Dec 2013
Why not try normalized cross correlation, if you have the Image Processing Toolbox. Attached below is a demo.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!