matlab中dim,matlab中沿nd数组维度的平均值

这是一个使用 accumarray 函数的通用解决方案 . 我没有测试它有多快 . 可能还有一些改进的余地 .

基本上,accumarray将x中的值按照您的问题的自定义索引矩阵进行分组

x = reshape(1:30*30*20*300,30,30,20,300);

s = size(x);

% parameters for averaging

dimAv = 4;

n = 10;

% get linear index

ix = (1:numel(x))';

% transform them to a matrix of index per dimension

% this is a customized version of ind2sub

pcum = [1 cumprod(s(1:end-1))];

sub = zeros(numel(ix),numel(s));

for i = numel(s):-1:1,

ixtmp = rem(ix-1, pcum(i)) + 1;

sub(:,i) = (ix - ixtmp)/pcum(i) + 1;

ix = ixtmp;

end

% correct index for the given dimension

sub(:,dimAv) = floor((sub(:,dimAv)-1)/n)+1;

% run the accumarray to compute the average

sout = s;

sout(dimAv) = ceil(sout(dimAv)/n);

y = accumarray(sub,x(:), sout, @mean);

如果您需要更快,更高效的内存操作,则必须编写自己的mex函数 . 我想,这应该不会那么困难!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值