You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab表格(Table)列收益率计算问题及解决方案

Matlab表格列收益率计算问题及解决方法

原始表格

31/01/2023    01/02/2023    02/02/2023     03/02/2023 
02/01/2023   202,00     228,5   219 198
03/01/2023   195,00     215     206 206
04/01/2023   182,00     196     191 185
05/01/2023   185,00     202     200 198
06/01/2023   184,00     206     200 198
07/01/2023   182,00     207     207 199
08/01/2023   180,00     199     195 192
09/01/2023   179,00     192     189 184

需求

计算每一列的收益率,即每列中当前行的值除以上一行的值(例如195/202、182/195等)。

尝试的错误代码

for col = 2:size(ret_table, 2)
    for row = 2:size(ret_table, 2)
        current_value = ret_table{row, col};
        previous_value = ret_table{row-1, col};
            if isnumeric(current_value) && isnumeric(previous_value)
                ret_table{row, col} = (current_value / previous_value);
            end
    end
end

遇到的错误

  • 使用/运算符时:Arguments must be numeric, char, or logical.
  • 使用./运算符时:Both inputs must have the same row labels.

预期输出

31/01/2023   01/02/2023  02/02/2023  03/02/2023
02/01/2023  NaN NaN NaN NaN
03/01/2023  -0,04   -0,06   -0,06    0,04 
04/01/2023  -0,07   -0,09   -0,08   -0,11 
05/01/2023   0,02    0,03    0,05    0,07 
06/01/2023  -0,01    0,02    -       -   
07/01/2023  -0,01    0,00    0,03    0,01 
08/01/2023  -0,01   -0,04   -0,06   -0,04 
09/01/2023  -0,01   -0,04   -0,03   -0,04 

解决代码

ret_table = array2table(PUN_fwd);

for col = 2:size(ret_table, 2)
    for row = 2:size(ret_table, 1) 
        current_value = table2array(ret_table{row, col});
        previous_value = table2array(ret_table{row-1, col});
        Returns{row, col} = log1p(current_value / previous_value);
    end
end

内容的提问来源于stack exchange,提问作者Medea00

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 14:32:35