Hi again.
I have a dataset with 100 rows of data with 4 columns.
I am trying to code the following and would like some help please:
For each row
If Table1.column1 = 'FOREX' (which is a string) and Table1.column4 < 0
then return Table1.column3 in Table1.column2
otherwise
If Table1.column1 = 'FOREX' (which is a string) and Table1.column4 >=0
then return Table1.column2 = (1% of Table1.column3) + Table1.column4
End

 채택된 답변

dpb
dpb 2014년 4월 15일

1 개 추천

One (of many) way(s) to write it...
ix=strcmp(T.c1,'FOREX') & T.c4<0;
T(ix).c2=T(ix).c3;
ix=strcmp(T.c1,'FOREX') & T.c4>=0;
T.c2(ix)=0.01*T(ix).c3+T1(ix).c4;
Names abbreviated to protect...

추가 답변 (1개)

Matthew
Matthew 2014년 4월 16일

0 개 추천

The above worked fine for numeric but I'm having a similar problem when I am wanting replace a field with text in the dataset. Can you help please?
iy=or(strcmp(CtpyList.deal_type,'120'), strcmp(CtpyList.deal_type,'123'));
This works fine and creates the table iy which details 0s or 1s when it meets the above condition.
CtpyList.customer_nr(iy) = CtpyList.Customer_nr_Sec(iy);
This works but it concatenates the cell i.e. customer_nr(iy) now reads: 1 NaN
Note that NaN has been converted to string.
Any ideas on how to totally replace this string NaN with the field in the corresponding row for Customer_nr_Sec??

댓글 수: 6

dpb
dpb 2014년 4월 16일
ly isn't a table, it's a logical array (vector). Need to keep class types straight or you run into the problems below.
What are the types of ...nr and ...nr_Sec, respectively? You can't mix and match types or, as you've discovered, Matlab either chokes or sometimes will try to do an implicit cast for you...
Matthew
Matthew 2014년 4월 16일
Hi dpb,
Yes sorry - iy is a logical array.
customer_nr and Customer_nr_Sec are column headers in datasets and the data in these cells are both string (even the NaN which is currently present in customer_nr due to a num2str performed in a previous script).
dpb
dpb 2014년 4월 16일
편집: dpb 2014년 4월 16일
Probably your problem is that they're character strings, maybe? I can't see your data itself but what does
CtpyList.customer_nr(iy,:) = CtpyList.Customer_nr_Sec(iy,:);
do? Does that solve the problem?
If not, post back with the results of
whos CtpyList.customer_nr(iy)
whos CtpyList.Customer_nr_Sec(iy)
for some location in iy -- say the first T would be ok.
Matthew
Matthew 2014년 4월 16일
dpb - you beauty!
This first line of code you sent me does the trick - thank you again!
dpb
dpb 2014년 4월 16일
Good; thought it might.
You do understand why, then???
Matthew
Matthew 2014년 4월 17일
Yes

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

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

질문:

2014년 4월 15일

댓글:

2014년 4월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by