How to use a loop for an array
1 回表示 (過去 30 日間)
古いコメントを表示
My question is:
I have an array with values ranging from 0 to 200 something, and I need to find the average value for a set range of the data i.e. I need to find the average value for 0 to 1, the average value of the data for 1-2, and so on until it hits the end. I would like to save that information in a new matrix that I can look up later on.
Any help is appreciated.
2 件のコメント
James Tursa
2016 年 3 月 23 日
What about the boundary points? E.g., if a value is exactly 1, do you want it to be part of the 0-1 average and also the 1-2 average? Or do you want these averages to be mutually exclusive?
採用された回答
Image Analyst
2016 年 3 月 24 日
If you have the Statistics and Machine Learning Toolbox, you can use grpstats(). It's a single line of code:
% Create sample data
r = 200 * rand(100000, 1);
% Now find means in every integer range.
means = grpstats(r, floor(r))
4 件のコメント
Image Analyst
2016 年 3 月 26 日
In my code above, means is already an array. Do you mean like an array of those arrays? Like you want a 2D array or an array of structures?
その他の回答 (1 件)
James Tursa
2016 年 3 月 23 日
編集済み: James Tursa
2016 年 3 月 23 日
Does something like this do what you want?
n = ceil(max(values));
x = bsxfun(@ge,values(:),0:n-1) & bsxfun(@le,values(:),1:n);
averages = sum(bsxfun(@times,values(:),x)) ./ sum(x);
averages(isnan(averages)) = 0;
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!