# Vectorize multiple t tests across matrices

조회 수: 1(최근 30일)
Benjy Barnett 2021년 3월 11일
답변: Abdolkarim Mohammadi 2021년 3월 12일
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!

댓글을 달려면 로그인하십시오.

### 답변(1개)

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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by