# Error in paired t-test between corresponding elements.

2 visualizzazioni (ultimi 30 giorni)
Haya Ali il 25 Mar 2024
Commentato: Haya Ali il 26 Mar 2024
I am trying to perform paired t-test between corresponding elements of group1 and group2. It should provide non-NaN p-values for each corresponding pair but it is providing NAN values. Please help to resolve the error. Below is my code.
% Define the data for two groups
group1 = [10, 15, 20, 25, 30];
group2 = [12, 18, 22, 28, 32];
% Initialize array to store p-values
p_values = zeros(size(group1));
% Check if the sizes of the two groups are the same
if numel(group1) ~= numel(group2)
error('The sizes of the two groups must be the same.');
end
% Perform paired t-test for each pair of corresponding elements
for i = 1:numel(group1)
fprintf('Performing t-test for Node %d...\n', i);
% Perform paired t-test for the current pair
[~, p_values(i)] = ttest(group1(i), group2(i));
end
% Display the p-values for each corresponding pair
for i = 1:numel(group1)
fprintf('Node %d: p-value = %f\n', i, p_values(i));
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
VBBV il 26 Mar 2024
The input data for samples need to be vectors when using ttest or ttest2 functions to evaluate the p-values

Accedi per commentare.

### Risposta accettata

VBBV il 25 Mar 2024
Modificato: VBBV il 25 Mar 2024
% Define the data for two groups
group1 = [10, 15, 20, 25, 30];
group2 = [12, 18, 22, 28, 32];
% Initialize array to store p-values
p_values = zeros(size(group1));
% Check if the sizes of the two groups are the same
if numel(group1) ~= numel(group2)
error('The sizes of the two groups must be the same.');
end
% Perform paired t-test for each pair of corresponding elements
for i = 1:numel(group1)
fprintf('Performing t-test for Node %d...\n', i);
% Perform paired t-test for the current pair
[~, p_values(i)] = ttest(group1, group2(i)); % use a vector for paired comparison test
end
Performing t-test for Node 1... Performing t-test for Node 2... Performing t-test for Node 3... Performing t-test for Node 4... Performing t-test for Node 5...
% Display the p-values for each corresponding pair
for i = 1:numel(group1)
fprintf('Node %d: p-value = %f\n', i, p_values(i));
end
Node 1: p-value = 0.086418 Node 2: p-value = 0.601832 Node 3: p-value = 0.601832 Node 4: p-value = 0.086418 Node 5: p-value = 0.027426
p_values
p_values = 1x5
0.0864 0.6018 0.6018 0.0864 0.0274
##### 4 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
VBBV il 26 Mar 2024
Hi @Haya Ali, for two sample t-test, check the ttest2 function instead of regular ttest
Haya Ali il 26 Mar 2024
Thank you!

Accedi per commentare.

### Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by