How to multiply two columns of the same table?

58 visualizaciones (últimos 30 días)
Bengtman
Bengtman el 1 de Mayo de 2021
Comentada: Eric Sofen el 3 de Mayo de 2021
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 comentarios
Stephen23
Stephen23 el 2 de Mayo de 2021
You need to use curly braces to acces the table content. Read this to know more:
Eric Sofen
Eric Sofen el 3 de Mayo de 2021
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

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by