How to frormat Table to use Previous Values for Calculations

조회 수: 1 (최근 30일)
Anthony Koning
Anthony Koning 2021년 12월 14일
답변: Cris LaPierre 2021년 12월 14일
Does anyone know how to format a table to use the previous values of a column to calculate the a new value. For example, using a base code of
x = (0:1:9)'
y = (2.*x)
T = table (x, y)
where I would like to subtract the previous value of Y to calculate a new Y value of:
x = (0:1:9)'
y = (2.*x-y)
T = table (x, y)
Is only producing 0s for the new Y value, whereas the table that looks like this is the desired outcome. The initial values of Y (used to calculate the first Y value) is 0
x = (0:1:9)'
y = [0; 2; 2; 2; 2; 2; 2; 2; 2; 2]
T = table (x, y)

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 12월 14일
I would use the table variable T
% original table
x = (0:9)';
y = (2*x);
T = table (x, y)
T = 10×2 table
x y _ __ 0 0 1 2 2 4 3 6 4 8 5 10 6 12 7 14 8 16 9 18
% New Y values
T.y=(2*T.x-T.y)
T = 10×2 table
x y _ _ 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0
The reason the new y values are all zero is because, in the original table, y is 2*x, so your new equation is really , which equals 0. You need a different equation to get the result you want.

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by