How to maintain original indexing while applying multiple constraints?
2 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!