How to multiply two columns of the same table?

조회 수: 20 (최근 30일)
Bengtman
Bengtman 2021년 5월 1일
댓글: Eric Sofen 2021년 5월 3일
Hello, I have a set of data with two columns: "Item_price" (column 5) , "Item_cnt_day" (column 6)
I want to multiply these to columns in order to get a new column "revenue":
When I use the operator ".*", I only get the debug result that the operator doesn't work for tables.
This is the codeline I got right now:
df_train.revenue = df_train(:,5).*df_train(:,6)
  댓글 수: 2
Stephen23
Stephen23 2021년 5월 2일
You need to use curly braces to acces the table content. Read this to know more:
Eric Sofen
Eric Sofen 2021년 5월 3일
Parens indexing creates a table of a subset of the data. Curly braces or dot indexing access the contents of a table. These all do the same thing:
df_train.revenue = df_train{:,5}.*df_train{:,6}
df_train.revenue = df_train.(5).*df_train.(6)
df_train.revenue = df_train.Item_price.*df_train.Item_cnt_day

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

채택된 답변

David Fletcher
David Fletcher 2021년 5월 1일
Use curly brace to extract the column data as matrices
df_train.revenue = table(df_train{:,5}.*df_train{:,6})
  댓글 수: 1
Bengtman
Bengtman 2021년 5월 2일
Worked well for me without writing "Table" upfront! Thanks for the fast answer :)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by