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

45 visualizaciones (últimos 30 días)
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 comentarios
Thishan Dharshana Karandana Gamalathge
Editada: dpb el 17 de Jun. de 2017
A=[2 5 NaN 10 NaN 1 NaN 20 1 10];
B=[Nan 3 5 50 NaN 1 89 11 5 9];
1st I had to get rid of pairs which had NaN in either A or B. So I got A1 and B1 to be
A1=[5 10 20 1 10]; B1=[3 50 11 5 9];
Then I removed data from B which are greater than 10. So I left with
B2=[3 5 9].
My question is how to get corresponding values from A1. ie. how to get
A1=[5 1 10]?
Here I used only a vector size of 10 rather than 20 just to explain clearly.
dpb
dpb el 18 de Jun. de 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
>>

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 17 de Jun. de 2017
Editada: dpb el 18 de Jun. de 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.
ADDENDUM
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);
  7 comentarios
dpb
dpb el 18 de Jun. de 2017
Because you mixed metaphors ... I transposed back from the columns to rows; you already had rows. So you end up with one long column above instead of two. To use your original, don't transpose. (Check the intermediate results and see where it went wrong)
(I presumed you'd recognize that since my previous solution was based on columns when said to "turn 'em around" I was meaning that was from the previous condition to return to your orientation, sorry.)
dpb
dpb el 18 de Jun. de 2017
Editada: dpb el 18 de Jun. de 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....

Iniciar sesión para comentar.


Thishan Dharshana Karandana Gamalathge
A(ix&B<=10) shows "Error using & Matrix dimensions must agree"

Categorías

Más información sobre Statistics and Linear Algebra en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by