Obtain logical vector for the position of the values located above and below NaNs

1 view (last 30 days)
Hi guys,
I have a pretty simple question. I would like to obtain the logical vector indicating the indices of numerical values that lie above and below NaNs. This way, my goal would be to construct a logical vector that gives me the position of a NaN only if it has a numerical value on top and below it.
For example:
xq = [5 3 nan 2 8 nan 3 3 2 8 nan nan nan 4 nan ]' ;
The output I would like to get is
idx = [0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 ]' ;
Thanks for your answer in advance,
KMT.

Accepted Answer

Ameer Hamza
Ameer Hamza on 16 Jun 2018
Try this
xq = [5 3 nan 2 8 nan 3 3 2 8 nan nan nan 4 nan]';
nanIndex = isnan(xq);
requiredIndex = strfind(nanIndex', [0 1 0])+1; % first argument must be row vector
This will give you numeric index. To get the logical vector, add the following lines to this code
requiredResultLogical = false(size(xq));
requiredResultLogical(requiredIndex) = true;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!