# Filtering out indices of vector A based on restrictions applied to vector B

60 views (last 30 days)
Edited: dpb on 18 Jun 2017
Suppose i have to vectors A and B or size [1,20]. After filtering out cases with NaNs on each I will get 10 good pairs of data from A and B. Then I have to apply a restriction to B, such as finding data points which are lower than a particular number. I was able to do that, but now i am stuck with how to get corresponding value from A, for the values of B which satisfied the restriction.
##### 2 CommentsShowHide 1 older comment
dpb on 18 Jun 2017
That is what the Answer does...you don't need to do anything except use the addressing vector as shown; no need to decimate the arrays at all.
>> ix=all(isfinite([A B]),2); % the rows with neither NaN
>> [A(ix) B(ix)] % illustrate -- you missed one by hand
ans =
5 3
10 50
1 1
20 11
1 5
10 9
>>
Now use the above o pick the condition...
>> A(ix&B<=10) % all finite and B<=10 from the original A/B arrays
ans =
5
1
1
10
>>
It's possible to do away with the temporary ix as well, but keeping track of parens and colons and all can be tricky at first.
But, just for showin' off... :)
>> A(all(isfinite([A B]),2)&B<=10)
ans =
5
1
1
10
>> find(all(isfinite([A B]),2)&B<=10) % if need to know the rows...
ans =
2
6
9
10
>>

dpb on 17 Jun 2017
Edited: dpb on 18 Jun 2017
ix=B<thresh; % logical addressing vector of the condition
C=A(ix); % use it to access A
you can, of course, dispense with the temporary unless have need for it for additional addressing operations as
C=A(B<thresh);
The condition can be anything that reduces to a logical array of the same length as the array or actual indices from lookup functions such as find.
The from root arrays solution w/o parsing down the original arrays that the above assumes start with--
ix=all(isfinite([A B]),2); % the rows with neither NaN
A(ix&B<=THRSHOLD); % all finite and B<=threshold from originals
It's possible to do away with the temporary ix as well...
A(all(isfinite([A B]),2)&B<=THRSHOLD);
dpb on 18 Jun 2017
Edited: dpb on 18 Jun 2017
BTW, the distinction of whether they're row or column vectors arises only because I was lazy and didn't want to write
isfinite(A) & isfinite(B)
while writing code at command line, so I concatenated A, B to do the test on the combined array and then checked the joint condition with all. There's where the rub arises--if they're columns want to horzcat, if rows only vertcat gives the right answer; otherwise, as you found out, you get just one long column vector and the subsequent test, while accurate for what it was, isn't testing "the right stuff".
If one writes the explicit logical expresssion, then it is immaterial which way they are, only that both must be the same orientation.
Writing code "in anger" I'd've probably used [A(:) B(:)] to ensure the orientation but I tend to write minimalist code here given the time constraints....

A(ix&B<=10) shows "Error using & Matrix dimensions must agree"
dpb on 18 Jun 2017
You made as mistake in application, then. See above...

### Categories

Find more on Model Compatibility in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by