refer to the value of a variable referenced by the value of another variable

1 次查看(过去 30 天)
Suppose I have a table
date n_visitor n_reference_visitor
------------------------------------
January-1-2020 100
January-2-2020 50
January-3-2020 25
I want to do something like
if date is before January-2-2020, then the value of a variable "n_reference_visitor= the value of n_reference_visitor at January-2-2020
That is, I want to have
date visitor n_reference_visitor
--------------------------------------------------------------
January-1-2020 100 100 50
January-2-2020 50 50 50
January-3-2020 25 25
Please advise.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-11-30
datetime() supports several of the operators supported by numeric types, including comparison operators. For example, check the following code
dts = datetime(2020,1,1)+(0:2);
T = table(dts.', [100; 50; 25], [0; 0; 0], 'VariableNames', {'date', 'n_visitor', 'n_reference_visitor'});
ref_date = datetime(2020,1,2); % January-2-2020
val = T.n_visitor(T.date==ref_date);
T.n_reference_visitor(T.date<=ref_date) = val;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by