3d 数组中非零元素的均值 [英] Mean of non-zero elements in 3d array

查看:60
本文介绍了3d 数组中非零元素的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 i x j x k 3d 矩阵(这是一部电影).在没有循环的情况下,我试图取每个 ixj 数组中非零正元素的平均值,并将这些值放入一个 1x1xk 矩阵中.

I have this i x j x k 3d matrix (it's a movie). Without loops, I'm trying to take the mean of the non-zero positive elements in each ixj array and put these values into a 1x1xk matrix.

我已经搜索了很长时间了,虽然有很多解决方案可以为 2d 矩阵实现这一点,但我一生都无法找到一种方法来为 3d 矩阵做到这一点而不使用循环.

I've been searching for quite a while now, and although there's plenty of solutions to accomplish this for a 2d matrix, I can't for the life of me find a way to do it for a 3d matrix without using a loop.

推荐答案

如果将每个图像(帧)转换为数组会怎样:

What if you convert each image (frame) into an array:

% Remove negative and zero elements
A(A<=0) = 0;
% Convert each image into array
B = reshape(A,[Nimg,Nfrm]);
% Extract mean value of each image
C = mean(B,1);

其中 Nimg 是每个图像中的像素数,Nfrm 是图像数.

where Nimg is the number of pixels in each image and Nfrm is the number of images.

如果您不想在均值分母中包含非零和负数(如@Dan 建议的那样),只需使用以下行缩放结果:

If you don't want to include the non-zero and negative numbers in the mean denominator (as @Dan suggests), just scale the result with the following line:

C_Dan = C.*squeeze(Nimg./sum(sum(A>0))).';

这篇关于3d 数组中非零元素的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆