Error in paired t-test between corresponding elements.

3 views (last 30 days)
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 Comment
VBBV
VBBV on 26 Mar 2024
The input data for samples need to be vectors when using ttest or ttest2 functions to evaluate the p-values

Sign in to comment.

Accepted Answer

VBBV
VBBV on 25 Mar 2024
Edited: VBBV on 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 Comments
VBBV
VBBV on 26 Mar 2024
Hi @Haya Ali, for two sample t-test, check the ttest2 function instead of regular ttest

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!