From: CuppoJava on
Hi,
I just started using Mathematica after a year of experience with
Matlab, and I have a question regarding how to handle matrices.

What is the equivalent code for doing
sum(A,3) in mathematica?
This sums all the elements A along the third dimension.

I'm having a hard time finding it in the documentation.

Thanks a lot for your help
-Patrick

From: Murray Eisenberg on
A matrix is 2-dimensional and has no "third dimension". Are you trying
to sum along the 3rd row? Along the 3rd column?

Or are you talking about a higher-dimensional array?

CuppoJava wrote:
> Hi,
> I just started using Mathematica after a year of experience with
> another system, and I have a question regarding how to handle matrices.
>
> What is the equivalent code for doing
> sum(A,3) in mathematica?
> This sums all the elements A along the third dimension.
>
> I'm having a hard time finding it in the documentation.
>
> Thanks a lot for your help
> -Patrick
>

--
Murray Eisenberg murray(a)math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

From: Szabolcs Horvát on
On 2009.10.26. 5:26, CuppoJava wrote:
> Hi,
> I just started using Mathematica after a year of experience with
> another system, and I have a question regarding how to handle matrices.
>
> What is the equivalent code for doing
> sum(A,3) in mathematica?
> This sums all the elements A along the third dimension.
>


This is controlled by the second argument of Total. Total[a, {3}]
(depending on what you mean by 'third dimension')

From: Nasser M. Abbasi on

"CuppoJava" <patrickli_2001(a)hotmail.com> wrote in message
news:hc38cr$i5b$1(a)smc.vnet.net...
> Hi,
> I just started using Mathematica after a year of experience with
> another system, and I have a question regarding how to handle matrices.
>
> What is the equivalent code for doing
> sum(A,3) in mathematica?
> This sums all the elements A along the third dimension.
>
> I'm having a hard time finding it in the documentation.
>
> Thanks a lot for your help
> -Patrick
>

another system:
---------
>> A

A(:,:,1) =

1 2
3 4

A(:,:,2) =

5 6
7 8

>> size(A)

2 2 2

>> sum(A,3)

6 8
10 12

Mathematica
---------------------
Dimensions[A = {{{1, 2}, {3, 4}},{{5, 6}, {7, 8}}}]
{2, 2, 2}

Total[A, 1]

{{6, 8}, {10, 12}}

--Nasser





From: Sjoerd C. de Vries on
Hi Patrick,

Many ways to do this, but the easiest way will be using Total. The
syntax variant you could use here is Total[A,{3}].

Perhaps you should check whether the ordering of the dimensions is the
same in both systems.

Cheers -- Sjoerd


On Oct 26, 6:26 am, CuppoJava <patrickli_2...(a)hotmail.com> wrote:
> Hi,
> I just started using Mathematica after a year of experience with
> another system, and I have a question regarding how to handle matrices.
>
> What is the equivalent code for doing
> sum(A,3) in mathematica?
> This sums all the elements A along the third dimension.
>
> I'm having a hard time finding it in the documentation.
>
> Thanks a lot for your help
> -Patrick