1
$\begingroup$

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?

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

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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;
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.