# Vectorize multiple t tests across matrices

5 views (last 30 days)
Benjy Barnett on 11 Mar 2021
I have two matrices of [18x650x650] dimensions. I would like to perform a ttest across the first dimension of these matrices at every element of the second and third dimension. So I should end up with 650*650 t tests in total. Specifically, I would like to do a t test of mat1(:,1,1) with mat2(:,1,1), followed by mat1(:,1,2) wit mat2(:,1,2) and so on.
So far I have this:
all_acc1 = rand(18,650,650);
all_acc2 = rand(18,650,650);
acc1_flat = zeros(18,650*650);
acc2_flat = zeros(18,650*650);
for subj = 1:size(all_acc1,1)
flat1 = reshape(squeeze(all_acc1(subj,:,:)).',1,[]);
flat2 = reshape(squeeze(all_acc2(subj,:,:)).',1,[]);
acc1_flat(subj,:) = flat1;
acc2_flat(subj,:) = flat2;
end
t_s = zeros(1,650*650);
p_s = zeros(1,650*650);
for i = 1:size(acc1_flat,2)
disp(i)
[h,p,ci,stats] = ttest(acc1_flat(:,i),acc2_flat(:,i));
t = stats.tstat;
t_s(i) = t;
p_s(i) = p;
end
But as you might imagine it takes several minutes to run. Is there a more efficient way to code this? Thanks!

Abdolkarim Mohammadi on 12 Mar 2021
ttest cannot be vectorized. I think the only speed boost left for you is to use parfor instead of for, as long as you have Parallel Computing Toolbox license.

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!