필터 지우기
필터 지우기

Make spceficic array values zero.

조회 수: 1 (최근 30일)
Santos García Rosado
Santos García Rosado 2021년 3월 18일
댓글: Santos García Rosado 2021년 3월 22일
Hi Mathworks community!
I'm having trouble trying to make some of my array values zero. This array corresponds to my Input:
Input = [-1 0 -1 0 0 NaN NaN NaN NaN -1 0 -1 -1 -1 0 NaN NaN NaN 0 0 -1 0 -1 NaN NaN NaN]
I'd like to make everything inside the array zero, but the last -1 before every NaN series. This should be the expected Output:
Output = [0 0 -1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 0]
Could someone please help me out?
Thank you,
Santos

채택된 답변

David Hill
David Hill 2021년 3월 18일
a=find(diff(isnan(Input))==1);
b=find(Input==-1);
Output=zeros(size(Input));
for k=a
Output(b(find(b<=k,1,'last')))=-1;
end
  댓글 수: 3
David Hill
David Hill 2021년 3월 18일
You could do something like:
Input = [-1 0 -1 0 0 NaN NaN NaN NaN -1 0 -1 -1 -1 0 NaN NaN NaN 0 0 -1 0 -1 NaN NaN NaN];
Output=zeros(size(Input));
idx=[];
for k=1:length(Input)
if Input(k)==-1
Output(k)=-1;
idx=[idx,k];
end
if isnan(Input(k))
Output(idx(1:end-1))=0;
idx=[];
end
end
Santos García Rosado
Santos García Rosado 2021년 3월 22일
Thank's for the quick feed back and help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by