For loop on two different dimensions, nested for loop?
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!