How can I run ttest2 on each entry of a correlation matrix (corr output) with multiple samples?

3 次查看(过去 30 天)
Hello, new to matlab and having trouble trying to write a for loop to calculate this.
I have two conditions with 8 subjects each in which I calcualted a correlation matrix between 40 observations leading me to have a 40x40 matrix for each subject. I have the matrixes in a cell for each condition. For example, a 1x8 CTRL cell and 1x8 TREATED cell. Each cell contains the 40x40 correlation matrix of a subject.
I want to run a ttest on each entry (e.g. CTRL(1,1) vs TREATED(1,1)) and output one 40x40 matrix in which significant different entries have 1's and nonsignificant entries have 0. I figure I would have to index each subjects entry into an column vector to run the ttest2, but I am having trouble. Help would be greatly appreciated.
Thanks

回答(1 个)

Paras Gupta
Paras Gupta 2023-11-17
Hi Andrew,
I understand that you want to run a t-test on each each entry of the correlation matrices in the 1x8 CTRL and 1x8 TREATED cell.
The following code illustrates how to achieve the same on randomly generated dummy data.
clear;
clc;
% Creating sample correlation matrices for 8 subjects in CTRL and TREATED conditions
CTRL = cell(1, 8);
TREATED = cell(1, 8);
for subject = 1:8
% Generating random 40x40 correlation matrix for each subject
correlationMatrix1 = rand(40);
correlationMatrix2 = rand(40);
% Storing the correlation matrix in the respective cell arrays
CTRL{subject} = correlationMatrix1;
TREATED{subject} = correlationMatrix2;
end
% Initialize the output matrix
outputMatrix = zeros(40);
% Iterate over each entry of the correlation matrices
for i = 1:40
for j = 1:40
% Generate arrays with (i, j)th entry from each subject's correlation matrix
ctrlValues = cellfun(@(x) x(i, j), CTRL);
treatedValues = cellfun(@(x) x(i, j), TREATED);
% Perform the t-test between the two arrays
[~, pValue] = ttest2(ctrlValues, treatedValues);
% Check if the p-value is significant (e.g., pValue < 0.05)
if pValue < 0.05
outputMatrix(i, j) = 1; % Mark as significant
end
end
end
% Resulting matrix
disp(size(outputMatrix));
40 40
Please refer to the follow documentation links for information on the functions used in the code above.
Hope this resolves your query.

Community Treasure Hunt

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

Start Hunting!

Translated by