Obtain logical vector for the position of the values located above and below NaNs
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Ameer Hamza
2018년 6월 16일
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;
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!