Calculating returns of a table structure and error operator '-' is not supported for operands of type 'table'.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Wietze Zijpp
el 3 de Abr. de 2022
Respondida: Walter Roberson
el 3 de Abr. de 2022
I have stock prices for 100 firms over 5000 days. Yielding a table of 5000x1000
Now I proposed calculating the column of returns for stock i by
Ri = (stock(2:end,i)-stock(1:end-1,i))./stock(1:end-1,i);
However I get the error
Operator '-' is not supported for operands of type 'table'.
Should I transform my table to a different format. Or is there a way to do this with a table?
0 comentarios
Respuesta aceptada
Walter Roberson
el 3 de Abr. de 2022
Ri = diff(stock{:,i}) ./ stock{1:end-1,i}
Or to vectorize,
Ri = diff(stock{:,:}) ./ stock{1:end-1,:}
Some people prefer to use
Ri = diff(table2array(stock)) ./ table2array(stock(1:end-1,:))
0 comentarios
Más respuestas (1)
the cyclist
el 3 de Abr. de 2022
Editada: the cyclist
el 3 de Abr. de 2022
You don't need to transform to a different variable class, but you need to use different syntax to access the contents of a table than the table itself. Specifically, you should use curly brackets. Try, for example
stock{1:end-1,i}
0 comentarios
Ver también
Categorías
Más información sobre Tables 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!