paired ttest multiple groups loop?

5 views (last 30 days)
kcin
kcin on 20 Jan 2017
Edited: Samayochita on 24 Apr 2025
Basic matlab user so assume there is an easy fix for my query, nonetheless looking for assistance on automating paired ttest between as many groups as available. Completing ttest, grouping p value results with group identifiers for sigstar boxplot. How can I loop this so it completes it no matter how many groups and not requiring me to enter them manually? (the groups and group identifiers) Thankyou for any assistance
[zh1,zp1]=ttest(z1,z2);
[zh2,zp2]=ttest(z1,z3);
[zh3,zp3]=ttest(z1,z4);
[zh4,zp4]=ttest(z1,z5);
[zh5,zp5]=ttest(z2,z3);
[zh6,zp6]=ttest(z2,z4);
[zh7,zp7]=ttest(z2,z5);
[zh8,zp8]=ttest(z3,z4);
[zh9,zp9]=ttest(z3,z5);
[zh10,zp10]=ttest(z4,z5);
ZP=[zp1,zp2,zp3,zp4,zp5,zp6,zp7,zp8,zp9,zp10];
ZH={[1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[2,5],[3,4],[3,5],[4,5]};
ZP(ZP>statvalue)=0;
ZPH=[num2cell(ZP);ZH];

Answers (1)

Samayochita
Samayochita on 24 Apr 2025
Edited: Samayochita on 24 Apr 2025
Hi kcin,
I understand that you are trying to automate paired t-tests across all unique group combinations, and prepare the results for use with sigstar boxplot, without hardcoding each pair.
Here is one way to achieve the desired result:
  • Combine all group data into a matrix Z (columns = groups)
  • Used nested for loops to iterate over all unique group pairs
  • Perform ttest() on each pair
  • Collect p-values (ZP) and their corresponding group indices (ZH)
  • Convert them into a structured format
ZPH = [num2cell(ZP(:)), ZH(:)];
to use with sigstar.
The relevant piece of code is given below:
ZP = [];
ZH = {};
index = 1;
for i = 1:4
for j = i+1:5
[~, p] = ttest(Z(:, i), Z(:, j));
ZP(index) = p;
ZH{index} = [i, j];
index = index + 1;
end
end
Hope this was helpful.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!