find() along a given dimension of a matrix

조회 수: 57 (최근 30일)
Oliver Johnson
Oliver Johnson 2016년 1월 25일
편집: Matt J 2016년 1월 27일
I have an n-by-m matrix, A. I would like to find the last element in each row of A satisfying some condition. Intuitively, I'm looking for a way to use the find() command along a given dimension of A like the following syntax:
k = find(X,n,direction,dimension)
where the syntax is the same as the current find() command, except there is an additional argument that tells MATLAB to perform the search along a specific dimension of the array X. And I would use it like
k = find(A < 5, 1, 'last', 2);
This would say, return the column index (dimension 2) of the last 1 element of each row of A satisfying the condition A < 5. I would expect k to be a column vector of length size(A,1) with the i-th element of k giving the desired column index.
Right now, I'm just using find() on each row inside of a for-loop:
for i = 1:size(A,1)
k(i) = find(A(i,:) < 5, 1, 'last');
end
but I'm trying to figure out a way to do this without a for-loop. For a small matrix the for loop wouldn't be a problem, but my matrix, A, has millions of rows, so the for-loop has millions of iterations and is pretty slow. Any suggestions?
  댓글 수: 1
jgg
jgg 2016년 1월 25일
This will work, but I think it's slower than looping.
table2array(rowfun(@(A)(find(A > 2,1,'last')),table(A)))

댓글을 달려면 로그인하십시오.

답변 (3개)

Matt J
Matt J 2016년 1월 25일
편집: Matt J 2016년 1월 25일
The following finds the final value in each row, but you can easily adapt it to other dimensions/directions.
function k = rowwiseLast(A)
%Finds locations k(i) of final non-zero value in each row A(i,:), with k(i)=NaN if
%the entire row is zero.
m=size(A,2);
[val,loc] = max( fliplr(logical(A)), [],2);
k=m+1-loc;
k(val==0)=nan;
end
If there is no value satisfying the criterion, than k(i)=nan for that row.
  댓글 수: 2
Oliver Johnson
Oliver Johnson 2016년 1월 27일
Hi Matt, the FEX submission that you gave the link for is substantially more complicated than your solution posted here. I prefer your apparently more concise and elegant solution, but I'm wondering what the difference is between yours and the FEX submission (i.e. is the FEX submission more robust)?
Matt J
Matt J 2016년 1월 27일
편집: Matt J 2016년 1월 27일
My solution will only find at most one occurrence in each row. The FEX submission truly is enabled with all the functionality of the FIND command, and can find n>1 occurrences if desired. Also, to implement direction='last', I need to apply the flip() command, which creates a theoretically unnecessary extra copy of the input array. This can be avoided by using a MEX file, as the FEX version appears to do.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2016년 1월 25일
편집: Matt J 2016년 1월 25일

Image Analyst
Image Analyst 2016년 1월 25일
The for loop is fine. I mean, it's not like you have tens of millions of columns or anything do you?
  댓글 수: 2
Oliver Johnson
Oliver Johnson 2016년 1월 25일
Actually, I do have millions of rows, so my for-loop has millions of iterations.
Image Analyst
Image Analyst 2016년 1월 25일
Sorry, I meant rows since you're iterating over rows. Still might not be a problem even for millions of rows. How long is it taking? I just did 10 million iterations and it took 0.03 seconds, though I didn't have a find() in there.
Did you time jgg's suggestion?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by