Simple logical conditional flag column

조회 수: 1 (최근 30일)
david crowley
david crowley 2021년 11월 23일
답변: david crowley 2021년 11월 23일
I require help to develop some logic on the below data extract. I need the C2 column to toggle to '1' when C1 is '1'. Then, when C1 is '-1' the C2 column toggles to '0'. All rows in C2 between the C1 toggles (1 & -1) needs to turn to '1'.
Date C1 C2
'24-Dec-1999' 0 0
'31-Dec-1999' 1 0
'07-Jan-2000' 0 1
'14-Jan-2000' 0 1
'21-Jan-2000' 0 1
'28-Jan-2000' 0 1
'04-Feb-2000' -1 1
'11-Feb-2000' 0 0
'18-Feb-2000' 0 0
'25-Feb-2000' 0 0

채택된 답변

david crowley
david crowley 2021년 11월 23일
Figured it out
tt.C2 = cumsum(tt.C1);

추가 답변 (1개)

KSSV
KSSV 2021년 11월 23일
Simple, Let C1 , C2 be your two columns.
C2(C1==1)=1 ;
C2(C1==-1)=0 ;
  댓글 수: 1
david crowley
david crowley 2021년 11월 23일
편집: david crowley 2021년 11월 23일
Apologies, I re-read my original question and missed out some crucial information. That only changes the first and last entry (shown below). How do I make it so that the entries in between (7-Jan-2000 to 28-Jan-2000) in C2 are also listed as '1'?
Date C1 C2
'24-Dec-1999' 0 0
'31-Dec-1999' 1 1
'07-Jan-2000' 0 0
'14-Jan-2000' 0 0
'21-Jan-2000' 0 0
'28-Jan-2000' 0 0
'04-Feb-2000' -1 0
'11-Feb-2000' 0 0
'18-Feb-2000' 0 0
'25-Feb-2000' 0 0

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

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by