Creating a loop to use all columns in a dataset array
古いコメントを表示
Hello , i have a data set with 10 columns.The first column contains the names of the variables and the 9 next contain the data for the variables across 9 periods. I want , for each period ,to take the data which are lower than the data's median .I use the following command for the first period: s1=data(data.mv1<median(data.mv1),{'Name','mv1',}), where mv1 is the header of the first period's column and s1 a new dataset which contains only the variables i want. My question is , can i write a (for) loop that will automatically do this for the whole 9 periods, thus giving me s1,s2,...,s9?
1 件のコメント
Jan
2011 年 7 月 26 日
There must be one or two typos in "s1=data(data.mv1<median(data.mv1),{'Name','mv1',})"
Please fix them by editing your question.
採用された回答
その他の回答 (2 件)
Jan
2011 年 7 月 26 日
1 投票
It would be easy, if you do not use symbols like 's1' and 'mv1', which have an index inside the name. Better use and index as index: s{1}, s{2}, ... and mv{1}, mv{2}, ...
Titus Edelhofer
2011 年 7 月 26 日
Hi,
I guess this leads to the question, how to get to data.mv1 where mv1 is given in a variable?
Note, that data.mv1 is the same as data.('mv1'). So if you have e.g.
header = {'mv1', ...};
then you could do
for i=1:length(header)
col = data.(header{i});
% do your median thing
dataNew = data(col<median(col));
end
Hope, this helps,
Titus
2 件のコメント
Giorgos
2011 年 7 月 26 日
Titus Edelhofer
2011 年 7 月 26 日
Yes, header is a cell array containing the names. And yes, there should be some subscript depending on what further you want to do with the reduced data ...
カテゴリ
ヘルプ センター および File Exchange で Descriptive Statistics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!