필터 지우기
필터 지우기

Error in paired t-test between corresponding elements.

조회 수: 3 (최근 30일)
Haya Ali
Haya Ali 2024년 3월 25일
댓글: Haya Ali 2024년 3월 26일
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
VBBV
VBBV 2024년 3월 26일
The input data for samples need to be vectors when using ttest or ttest2 functions to evaluate the p-values

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

채택된 답변

VBBV
VBBV 2024년 3월 25일
편집: VBBV 2024년 3월 25일
% 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
VBBV
VBBV 2024년 3월 26일
Hi @Haya Ali, for two sample t-test, check the ttest2 function instead of regular ttest
Haya Ali
Haya Ali 2024년 3월 26일
Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by