# How to make lists all the same length by automatically removing every second element?

I have 20 files of matrices which all contain the same kind of data. I can import and take the single column I need out of every file into a list. I need to average each of the 20 file's lists together, row by row. However, some lists have better resolution than others, and are 200 elements long while others are 100. I want to automatically take every second element of the 200-element long lists, in order to average them with the 100-element long lists. This should return one single list of 100 numbers which is an average of all the 20 files.

I can sort out the long lists using Select[]. Is there an elegant way to take every second element of the long lists automatically, and then rejoin those now 100-element lists with the original 100-element lists to average them? Maybe a for loop or an if-statement?

• Try A[[;;;;2]], a shorter form of A[[1;;-1;;2]]. See also the documentation of Span. Apr 4 at 15:56
• Alternatively, you can also do 0.5 A[[1 ;; ;; 2]] + 0.5 A[[2 ;; ;; 2]] to average the even and odd entries. Apr 4 at 15:58
• I'm confused. Is A only my long lists, or all my lists together? Apr 4 at 16:13
• A is only one of the long lists. Apr 4 at 16:14

Rather than select and rejoin, you could just do a cleaning pass on your data. Let's make a cleaner function:

Cleaner[sample_] := sample /; Length[sample] == 100;
Cleaner[sample_] := Downsample[sample, 2] /; Length[sample] == 200


Let's say your ragged data, after the import is stored in a variable data. We can use our cleaner like this:

cleanData = Cleaner /@ data


And then, of course, take the mean index-wise:

Mean[cleanData]


This is a bit too hard-coded for my tastes. If your sample lengths are not either 100 or 200 exactly, you'll have to make adjustments. So, I'd be tempted to use ArrayResample in the Cleaner method, and maybe add a length argument to the Cleaner method. I'm not exactly sure what your requirements are, but it might look something like this:

Cleaner[length_, sample_] := ArrayResample[sample, length]


And

Cleaner[100, #] & /@ data


Of course, in this approach, we're not getting much by introducing the Cleaner method, so you might just do

ArrayResample[#, 100] & /@ data


Be aware that this will lead to a different result after applying Mean, but as I said, I'm not sure what your requirements are.

Maybe this would work. I cannot test it because I don't have example data.

result = ConstantArray[0., {100, m, n}];
Do[
A = Import["file_" <> IntegerString[i, 10, 6] <> ".dat", "Data"];
result += If[OddQ[i], A, A[[;; ;; 2]]];
, {i, 1, 20}];

result /= 20;