Matlab中有聚合数组函数吗?是否可以在这样的数组函数中引用不同的索引?

问题描述 投票:0回答:1

Matlab中有没有类似Google Sheet中的

reduce
或Racket中的
foldr
的聚合数组函数?

例如,前几天,我希望提取/排除代表该月每第三周的行,

isnumeric()
=> 正确
isnumeric()
=> 正确
202211 要聚合的数据类型_1
202211 要聚合的数据类型_1
202211 要聚合的数据类型_2
202211 要聚合的数据类型_1
202211 要聚合的数据类型_1
202212 要聚合的数据类型_1
202212 要聚合的数据类型_1
202212 要聚合的数据类型_2
202212 要聚合的数据类型_1

我最终将数据复制到 Google Sheet,其中可以使用一个(本地)函数处理数据,然后才复制到 Matlab

同样,在上面的示例中,有必要(?)能够根据当前索引的预设偏移量来引用不同的索引。为了判断一行是否代表一个月的第三周,一种方法是检查月份数字是否与之前的 3 行不同但与之前的 2 行相同。 Google Sheet 通过

offset
可以做到这一点。 (Google Sheet
reduce
也只接受一维数组作为参数,进一步增加了对
offset
的需求。)

那么..是否有

reduce
foldr
的 Matlab 等效工具以及相应的工具,例如
offset

arrays matlab
1个回答
0
投票

您可以使用月份数组的

diff
(与之前示例的差异)来查找它何时发生变化,即差异不为零的地方。然后您
find
这些指数并向其中添加两个以抵消 2 周。

idx = find([true; diff(m)~=0])+2;

如果您想在示例中生成第二列,那么完整的代码将类似于

% Example input data
m = [202211, 202211, 202211, 202211, 202211, 202212, 202212, 202212, 202212];

% Find indices where month changes, offset by two weeks.
% First sample is always true as we want to count the first week in the input
% and "diff" returns an array one element shorter than the input
dm = find([true; diff(m(:))~=0])+2;

% Create an output with type "1" as the default and "2" for the 3rd-week indices
dm( dm > numel(m) ) = []; % Handle edge case where the offset exceeded the array size
aggType = ones(size(m));
aggType(dm) = 2;
© www.soinside.com 2019 - 2024. All rights reserved.