如何在 MATLAB 中每 12 个月取一次中位数?
how to take the median every 12 monhts in MATLAB?
我有以下数据框:
x = randn(216,1)
我怎样才能得到每 12 个中位数 months/rows?
我需要一个 18x1 的向量。
谁能帮帮我?
使用 For 循环并按块大小递增,12
在每次迭代中一次获取数组的一块。检索到的数组的分区范围从值 Median_Index
到它后面的 11 个索引。
x = randn(216,1);
Median_Values = zeros(18,1);
Index = 1;
for Median_Index = 1: +12: 216
Chunk = x(Median_Index: Median_Index + 11);
Median_Values(Index) = median(Chunk,'all');
Index = Index + 1;
end
注意:all
可以删除,因为我们正在处理一个向量(特别是因为它是一维的)感谢@Luis Mendo 获取建议和兼容性提示。
运行 使用 MATLAB R2019b
你只需要reshape
your data as a 12-column matrix, and then use median
带第二个参数来指定你希望它沿着第一个维度进行操作,即计算每一列的中位数:
x = randn(216, 1); % example data
N = 12; % chunk size
result = median(reshape(x, N, []), 1);
我有以下数据框:
x = randn(216,1)
我怎样才能得到每 12 个中位数 months/rows?
我需要一个 18x1 的向量。
谁能帮帮我?
使用 For 循环并按块大小递增,12
在每次迭代中一次获取数组的一块。检索到的数组的分区范围从值 Median_Index
到它后面的 11 个索引。
x = randn(216,1);
Median_Values = zeros(18,1);
Index = 1;
for Median_Index = 1: +12: 216
Chunk = x(Median_Index: Median_Index + 11);
Median_Values(Index) = median(Chunk,'all');
Index = Index + 1;
end
注意:all
可以删除,因为我们正在处理一个向量(特别是因为它是一维的)感谢@Luis Mendo 获取建议和兼容性提示。
运行 使用 MATLAB R2019b
你只需要reshape
your data as a 12-column matrix, and then use median
带第二个参数来指定你希望它沿着第一个维度进行操作,即计算每一列的中位数:
x = randn(216, 1); % example data
N = 12; % chunk size
result = median(reshape(x, N, []), 1);