MathGroup Archive 2012

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

Search the Archive

Re: Thread::tdlen: Objects of unequal length in


You give an example of something that doesn't do what you want but you
never say what it is that you do want done.

Since Last[testdata] is a vector I assume that testdata is an m x n array,

testdata = Array[a, {3, 5}]

{{a[1, 1], a[1, 2], a[1, 3], a[1, 4], a[1, 5]}, {a[2, 1], a[2, 2], a[2, 3],
  a[2, 4], a[2, 5]}, {a[3, 1], a[3, 2], a[3, 3], a[3, 4], a[3, 5]}}

Your first function is

f1 = MapThread[Mean, Sequence[{{testdata}}]]

{{(1/3)*(a[1, 1] + a[2, 1] + a[3, 1]),
     (1/3)*(a[1, 2] + a[2, 2] + a[3, 2]),
     (1/3)*(a[1, 3] + a[2, 3] + a[3, 3]),
     (1/3)*(a[1, 4] + a[2, 4] + a[3, 4]),
     (1/3)*(a[1, 5] + a[2, 5] + a[3, 5])}}

What you have implemented is

f1 ==
 {Mean[testdata]} ==
   {Mean /@ Transpose[testdata]} ==
   {Total[testdata]/Length[testdata]}

True

Length[f1]

1

If what you want is the Mean of each row of testdata then use

meanByRows = Mean /@ testdata

{(1/5)*(a[1, 1] + a[1, 2] + a[1, 3] + a[1, 4] + a[1, 5]),
   (1/5)*(a[2, 1] + a[2, 2] + a[2, 3] + a[2, 4] + a[2, 5]),
   (1/5)*(a[3, 1] + a[3, 2] + a[3, 3] + a[3, 4] + a[3, 5])}

Length[meanByRows]

3

If you want the mean of each column then use

meanByColumns = Mean[testdata]

{(1/3)*(a[1, 1] + a[2, 1] + a[3, 1]),
   (1/3)*(a[1, 2] + a[2, 2] + a[3, 2]),
   (1/3)*(a[1, 3] + a[2, 3] + a[3, 3]),
   (1/3)*(a[1, 4] + a[2, 4] + a[3, 4]),
   (1/3)*(a[1, 5] + a[2, 5] + a[3, 5])}

Length[meanByColumns]

5

meanByColumns ==
 Mean /@ Transpose[testdata] ==
 Total[testdata]/Length[testdata] ==
 f1[[1]]

True

If what you want is the mean of all of the elements of testdata then use

meanOfAll = testdata // Flatten // Mean

(1/15)*(a[1, 1] + a[1, 2] + a[1, 3] + a[1, 4] + a[1, 5] +
      a[2, 1] + a[2, 2] + a[2, 3] + a[2, 4] + a[2, 5] +
      a[3, 1] + a[3, 2] + a[3, 3] + a[3, 4] + a[3, 5])

meanOfAll ==
  Mean[meanByRows] ==
  Mean[meanByColumns] //
 Simplify

True


Bob Hanlon


On Mon, Aug 20, 2012 at 4:14 AM, Aaron <aaron.sokolik at gmail.com> wrote:
> I'm new to Mathematica and trying to run rebuild some programs from other=
 systems in Mathematica.  I'm operating on large data lists and receiving t=
he unequal length error.  However, if I simply paste the output without the=
 extra curly bracket into an operation, everything works.  Obviously, copyi=
ng and pasting wont work for functions... How can I get around this?
>
> Below are the functions I'm using to generate two lists from a single dat=
aset:
>
> ln[13]:= MapThread[Mean, Sequence[{{testdata}}]]
> Out[13]= {{15.0059, 14.9897, 15.0248,....}}
>
> ln[14]:= Last[testdata]
> Out[14]= {14.9602, 14.8624, 15.3364, 15.0231,....}
>
> When I run Length on each of the outputs, I receive the proper number of =
datapoints for the Last function but receive an output of "1" for the Mean =
function.  If I paste the output with only 1 "{" though and run Length, I r=
eceive the proper output.
>
> I know this must be simple but I'm stuck.
>



  • Prev by Date: Re: How to make fitting code using NDSolve?
  • Next by Date: Re: Thread::tdlen: Objects of unequal length in
  • Previous by thread: Thread::tdlen: Objects of unequal length in
  • Next by thread: Re: Thread::tdlen: Objects of unequal length in