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

2 次查看(过去 30 天)
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?

回答(3 个)

dpb
dpb 2013-12-7
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 2013-12-7
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 个评论
dpb
dpb 2013-12-7
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.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by