总结一个没有for循环的4D矩阵 [英] Summing a 4D matrix without for loops in matlab

查看:146
本文介绍了总结一个没有for循环的4D矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小 m×n×p×q 的4D矩阵 A 。考虑 B = A(:,:1,1),它是一个 m×n 矩阵。我想总结 B 的所有元素来给出一个数字。我想为所有这样的 B 矩阵为所有 A 所以最后我将有一个 p q 矩阵。



如何在没有for循环的情况下执行此操作?作为一个3D矩阵的例子(例如 A 是一个3D矩阵),我认为这是有效的,

/ p>

  sum(squeeze(sum(A,1)),1)
pre>

但是我不知道如何做一个4D矩阵...

解决方案


squeeze(sum(a,[],p,q),1))



<或者,

  squeeze(sum(sum(A,2),1))


I have a 4D matrix A of size m × n × p × q. Consider B = A(:,:,1,1) which is an m × n matrix. I want to sum all the elements of B to give a number. I want to do this for all such B matrices for all A so finally I will have a p by q matrix.

How can i do this without for loops?

As an example for a 3D matrix (for example A be a 3D matrix) I think this works,

sum(squeeze(sum(A,1)),1)

But I don't know how to do this for a 4D matrix...

解决方案

what's wrong with

[m n p q] = size( A );
squeeze( sum( reshape( A, [], p, q ), 1 ) )

Alternatively,

squeeze( sum( sum( A, 2 ), 1 ) )

这篇关于总结一个没有for循环的4D矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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