help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Summing elements in an array


From: Andy Buckle
Subject: Re: Summing elements in an array
Date: Thu, 15 Jul 2010 12:33:40 +0100

On Thu, Jul 15, 2010 at 12:03 PM, dirac <address@hidden> wrote:
>
> Hi again,
>
> I really appreciate your answers. I have been looking up the sum function
> previously to posting but the terminology of the DIM part really stumped me
> (and many others seemingly). Andy: You guessed right, it is 2D and that
> looks like a really good way to minimise the amount of loops I need.
>
> Apologies for the lack of information, I think I'll be able to get on with
> it knowing how to use the sum function better :)
>
> Regards
> Martin

for a 2D array: dim 1, means sum columns. dim 2 means sum rows. I
should have mentioned before that to sum over 2D, you need to call sum
twice (end of example below).

>a=[1 2;3 4]
a =

   1   2
   3   4

>sum(a,1)
ans =

   4   6

>sum(a,2)
ans =

   3
   7

>sum(sum(a))
ans =  10

For arrays with more dimensions, it is similar. use this

three=floor(rand([2 2 2])*10)

to get a small 3D array to play with. summing with a DIM of 3 is left
as an exercise for the reader :o)

-- 
/* andy buckle */


reply via email to

[Prev in Thread] Current Thread [Next in Thread]