Filter matrix rows based on value of function in Matlab

조회 수: 2 (최근 30일)
ARS 2013년 2월 18일
댓글: yair suari 2015년 9월 1일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
yair suari 2015년 9월 1일
seems like ismember will do the work

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

채택된 답변

Mark Whirdy 2013년 2월 18일
편집: Mark Whirdy 2013년 2월 18일
Shown below using an anonymous function for neatness, but the trick is just to make sure that your function (anonymous, nested, sub, or separate) returns a LOGICAL VECTOR (or a vector of linear indexes)
fn = @(x)(x>1);
a = randn(20,1);
fn(a); % this would return a logical vector if you ran it
myfilteredVector = a(fn(a));
If I've misunderstood, could you give me an example with an input & a required output.
------
So the bones of the required operation is actually a row-wise matching? I do this by transforming each row into a single unique element by 10*A(:,1) + A(:,2), as below. Then ismember() with its mex engine can then be put to work on matching individual element. There are other matching/indexing approaches though but the primary aim is to have the function outputting a logical vector.
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;];
fn = @(x)(ismember(x,[12;13;24;34]));
% fn(10*A(:,1) + A(:,2)) would return a logical vector
A(fn(10*A(:,1) + A(:,2)),:); % logical vector selects rows, & we want every column so (logic, :)
You can of course also specify the matchset a=[12;13;24;34] as an argument as below.
fn = @(x,a)(ismember(x,a));
a = [12;13;24;34];
A(fn(10*A(:,1) + A(:,2)),:);
To be honest, your function is kind of unnecessary really (unless there's a reason outside of the example you've given), as you can just use ismember with the unique encoder 10*A(:,1)+A(:,2).
a = [1,2;1,3;2,4;3,4];
A( ismember(10*A(:,1)+A(:,2), 10*a(:,1)+a(:,2)) , :)
댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Mark Whirdy 2013년 2월 18일

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

추가 답변 (1개)

Azzi Abdelmalek 2013년 2월 18일
n=size(A,1);
A(arrayfun(@(x) expression(A(x,:)),(1:n)','un',0))=[]
댓글 수: 5이전 댓글 3개 표시이전 댓글 3개 숨기기
ARS 2013년 2월 18일
Sorry if I'm misunderstanding, but I receive the error:
Error using subsindex
Function 'subsindex' is not defined for values of class 'cell'.
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.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by