Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem: a matrix and a box...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27277] Re: [mg27268] Problem: a matrix and a box...
  • From: Tomas Garza <tgarza01 at prodigy.net.mx>
  • Date: Sun, 18 Feb 2001 02:52:12 -0500 (EST)
  • References: <200102170830.DAA17186@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Let mat be your n x m matrix. Try

In[1]:=
Table[Plus @@ (Part[mat, {i, i + 1}, {j, j + 1}] // Flatten), {i, 1,
    Dimensions[mat][[1]] - 1}, {j, 1, Dimensions[mat][[2]] - 1}]

Example:

In[2]:=
mat = {{a1, a2, a3, a4, a5}, {b1, b2, b3, b4, b5}, {c1, c2, c3, c4, c5},
{d1,
      d2, d3, d4, d5}, {e1, e2, e3, e4, e5}, {f1, f2, f3, f4, f5}};

In[3]:=
Table[Plus @@ (Part[mat, {i, i + 1}, {j, j + 1}] // Flatten), {i, 1,
    Dimensions[mat][[1]] - 1}, {j, 1, Dimensions[mat][[2]] - 1}]
Out[3]=
{{a1 + a2 + b1 + b2, a2 + a3 + b2 + b3, a3 + a4 + b3 + b4,
    a4 + a5 + b4 + b5}, {b1 + b2 + c1 + c2, b2 + b3 + c2 + c3,
    b3 + b4 + c3 + c4, b4 + b5 + c4 + c5}, {c1 + c2 + d1 + d2,
    c2 + c3 + d2 + d3, c3 + c4 + d3 + d4,
    c4 + c5 + d4 + d5}, {d1 + d2 + e1 + e2, d2 + d3 + e2 + e3,
    d3 + d4 + e3 + e4, d4 + d5 + e4 + e5}, {e1 + e2 + f1 + f2,
    e2 + e3 + f2 + f3, e3 + e4 + f3 + f4, e4 + e5 + f4 + f5}}

I don't claim this is the fastest but it works. For example, for a matrix
about the size you have (in a PC at 800 MHz),

In[4]:=
mat = Table[Random[Integer, {0, 9}], {3000}, {3005}];
In[4]:=
Table[Plus @@ (Part[mat, {i, i + 1}, {j, j + 1}] // Flatten), {i, 1,
        Dimensions[mat][[1]] - 1}, {j, 1,
        Dimensions[mat][[2]] - 1}]; // Timing
Out[59]=
{57.45 Second, Null}

Tomas Garza
Mexico City

----- Original Message -----
From: "Bedrosian Baol" <Bedrosian at MailAndNews.com>
To: mathgroup at smc.vnet.net
Subject: [mg27277] [mg27268] Problem: a matrix and a box...


> Hi
> this is the problem: I have a n x m matrix (with n and m ~3000 elements or
> more)
>
> a1 a2 a3..
> b1 b2 b3..
> c1 c2 c3..
> . . .
> . . .
>
> the algorythm is: compute the summ of the the a1, a2, b1 and b2 elements
(a
> ''gliding box'' 2x2), shifing the ''box'' for 1 column and repeat the sum
of
> the elements (now a2, a3, b2, b3), and so on...
> at the and of the first line, the box return on the second line and
> repeat...
>
> the final resuls would be another matrix like this:
>
> [a1+a2+b1+b2] [a2+a3+b2+b3]...
> [b1+b2+c1+c2] [b2+b3+c2+c3]...
> ...
>
> I'm not very experienced with Math4: there is a simple and easy way for
> reach
> the result?
>
> thanks
> Bed
>
>



  • Prev by Date: Re: Combination Algorithm without brut force - Combine 4's into least 6's
  • Next by Date: Re: Problem: a matrix and a box...
  • Previous by thread: Re: Problem: a matrix and a box...
  • Next by thread: Re: Problem: a matrix and a box...