How to maintain original indexing while applying multiple constraints?

2 views (last 30 days)
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.

Answers (1)

Star Strider
Star Strider on 28 Mar 2015
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.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!