For loop on two different dimensions, nested for loop?

조회 수: 6 (최근 30일)
Alexandra Santos
Alexandra Santos 2021년 3월 16일
댓글: Alexandra Santos 2021년 4월 6일
Hi there, I am trying to create a for loop on two different dimensions.
I have testdata, which is a 1 x 7 cell, and within each cell is a X x 23 table.
I would like to conduct a ttest_bonf test between 7 sets of 3 columns of each X x 23 table, and the column indices I specified by creating the 'column' matrix. E.g. test between column 3, 10 and 17, then a test between columns 4, 11, 18, etc.
How can I save the results of the ttest properly? I am not sure how to do this so I have just been throwing the f index in any possible place on the left side. I am new to Matlab
% create a test dataset similar to mine
id = {'one'; 'two' ;'three'; 'four'};
testtable1=[id array2table(rand(4,22))];
testtable2=[id array2table(rand(4,22))];
testtable3=[id array2table(rand(4,22))];
testdata = {testtable1, testtable2,testtable3}; % this is a 1 x 4 cell with a 4 x 23 table within
h= [];
p= [];
n = [3 10 17]; % create matrix of column numbers
column = [n; n+1; n+2; n+3; n+4; n+5; n+6];
for c=1:(size(testdata,2))
for f=1:size(column,1)
[h(c,:),p(c,:)]{f}=ttest_bonf(table2array([testdata{1,c}(:,column(f,1)) testdata{1,c}(:,column(f,2)) testdata{1,c}(:,column(f,3))]));
end
end

채택된 답변

Pavan Guntha
Pavan Guntha 2021년 3월 23일
I assume that the requirement is to store the 2 outputs of ttest_bonf (i.e., H, P) into a cell array. In that case the variable needs to be initialized as a cell array. The following code illustrates the idea:
h_p = {false(size(column,1), size(testdata,2)), zeros(size(column,1), size(testdata,2))};
h_pcell = {h_p, h_p, h_p};
The h_p denotes a cell array in which the first and second elements stores the 1st and 2nd outputs of 'ttest_bonf' function for each input.
h_pcell is also a cell array whose elements are of cell type. It is used to store the outputs of ttest_bonf function for the ‘testdata’ which is a 1x3 cell. The following code illustrates this:
for c=1:(size(testdata,2))
for f=1:size(column,1)
[h_pcell{c}{1}(f,:), h_pcell{c}{2}(f,:), ~] = ttest_bonf(table2array([testdata{1,c}(:,column(f,1)) testdata{1,c}(:,column(f,2)) testdata{1,c}(:,column(f,3))]));
end
end
You may refer to the documentation of cell indexing, multilevel indexing & table indexing for more information on initializing & indexing rules.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by