# question regarding sum(m,3) command

1 visualizzazione (ultimi 30 giorni)
fima v il 12 Dic 2022
Modificato: Jan il 13 Dic 2022
hello, i have a three dimentional matrices m.
output of sum(m,3) return a two dimentional matrices.
What kind of summing being done on my 3D matrices which creates the 2D matrices?
Thanks.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Torsten il 12 Dic 2022
Modificato: Torsten il 12 Dic 2022
It is summed over the third dimension of the matrix.
A = zeros(2,2,2);
A(:,:,1) = [1 2;3 4];
A(:,:,2) = [5 6;7 8];
sum(A,3)
ans = 2×2
6 8 10 12
A(:,:,1) + A(:,:,2)
ans = 2×2
6 8 10 12
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Jan il 13 Dic 2022
Modificato: Jan il 13 Dic 2022
Actually a sum over a 3D array along the 3rd dimension replies a [M x N x 1] array, as summing over the 2nd dimension creates a [M x 1 X P] array. But Matlab ignores trailing singelton dimensions, except it is the 2nd dimension of a matrix (known as "column vector").
You can even access these virtual dimensions:
X = [1, 2; 3, 4]
X = 2×2
1 2 3 4
X(1, 2, 1)
ans = 2
X(2, 2, 1, 1, 1, 1, 1)
ans = 4
There have been some inconsistencies in R2009a, which let trailing singelton dimensions exist for some functions implemented a C-mex. This did not cause bugs, but the ndims command counted this virtual dimensions also. In modern Matlab version, this feature is supported consequently as far as I know.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by