Reshape 3D array to match expected anova2 input

Alec Jacobson

December 19, 2013

weblog/

The anova2 function of MATLAB allows you to load in observations across two conditions with repetitions. The input format is a little funny though. I would expect to store this data in a 3D m-by-n-by-p array where m is the number of variants across condition B, n is the number of variants across condition A and p is the number of repetitions (actually I would expect the rows to be called "A" and columns "B" but help anova2 labels them this way, so I will, too). However, MATLAB expects repetitions to come as rows under each corresponding variant of condition B.

So to reshape a m-by-n-by-p matrix X use:

Y = reshape(permute(X,[3 1 2]),size(X,1)*size(X,3),size(X,2));

then you can call:

p = anova2(Y,size(X,3));

Update: Here's a nasty Anonymous Function:

to_anova = @(X) reshape(permute(X(:,:,:),[3 1 2]),size(X,1)*size(X,3)*size(X,4),size(X,2));

then you can call:

p = anova2(to_anova(X),size(X,3));