필터 지우기
필터 지우기

How to maintain original indexing while applying multiple constraints?

조회 수: 2 (최근 30일)
John Doe
John Doe 2015년 3월 28일
답변: Star Strider 2015년 3월 28일
I'm trying to identify the elements of a vector that meet several different inequality constraints. For a variety of reasons I can't apply all constraints in a single step.
I'm trying to accomplish this by finding all the elements that satisfy the first inequality, then attempting to evaluate ONLY those elements against the second inequality, and so forth. The problem is that I need to maintain a reference to the original vector's indexing. I'm attempting it like this:
Ans1 = find(X > 0)
Ans2 = find(X(Ans1) < 10)
Let's say X is a 100 element column vector and Ans1 = [1; 3; 5; 7; 9].
Then when I evaluate X(Ans1) < 10 I lose the indices of the original matrix that meet both conditions, because X(Ans1) is a 5x1 vector rather than a 'sparse' 100x1 vector containing only the elements that met the first constraint.
Is there a simple way to go about doing this? I understand for this example there are simple ways (find 0 < X < 10) but I have to apply the constraints separately, and need to only consider elements of my original vector that meet 'previous' constraints.
Any help is appreciated, Thanks.

답변 (1개)

Star Strider
Star Strider 2015년 3월 28일
You can apply both constraints at once with find:
Ans = find( (A > 0) & (A < 10) );
This satisfies both inequalities simultaneously, and preserves the original indexing.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by