Filter matrix rows based on value of function in Matlab
이전 댓글 표시
This seems like a simple question but I have been unable to find an answer anywhere. If I have a Matlab matrix "A" consisting of an arbitrary number of rows, how would I filter these rows based on the value of some function "f" (the argument of which is a row vector)? In other words, how would I keep only the rows of matrix A for which f is true? I tried
A(f(A(:)), :)
but to no success. Any help would be greatly appreciated.
So for example, if I have the matrix:
A =
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
4 1
4 2
4 3
4 4
And a function f that returns true only for inputs [1 2], [1 3], [2 4] and [3 4], my desired output would be:
ans =
1 2
1 3
2 4
3 4
댓글 수: 1
yair suari
2015년 9월 1일
seems like ismember will do the work
채택된 답변
추가 답변 (1개)
Azzi Abdelmalek
2013년 2월 18일
n=size(A,1);
A(arrayfun(@(x) expression(A(x,:)),(1:n)','un',0))=[]
댓글 수: 5
ARS
2013년 2월 18일
Azzi Abdelmalek
2013년 2월 18일
편집: Azzi Abdelmalek
2013년 2월 18일
expression(A(x,:))
%or
f(A(x,:))
but you have to specify your logical expression, (it's not a function)
ARS
2013년 2월 18일
Azzi Abdelmalek
2013년 2월 18일
편집: Azzi Abdelmalek
2013년 2월 18일
Try
n=size(A,1)
A(cell2mat(arrayfun(@(x) f(A(x,:)),(1:n)','un',0)),:)=[]
Also this is not a general solution, each expression has to be treated individually.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!