Iterating using a specific interval, to intermittently average data in a scatter plot.

Trying to iterate through two large sets of row data (in two columns, data.x and data.y) simultaneously, with the aim of averaging the data within these intervals so I can visualise a clearer scatter plot (see original plot below). I have provided a simplified idea of what I need in code below, However, I am running into the issue of the index not being an integer.
In the code below, I am trying to return the three averages of [1,2,3], [4,5,6] and [7,8,9] in each xmeans and ymeans array.
Would appreciate any tips on the best way to do this.
% test data
x = [1;2;3;4;5;6;7;8;9];
y = [1;2;3;4;5;6;7;8;9];
% means of storage of averages at each interval
xmeans = [];
ymeans = [];
% calculate uniform interval
distance_range = length(x);
interval = distance_range/3;
s = min(x);
while (s >= min(x)) && (s <= (max(x)+1))
for k = s : (s+interval)
xmeans(s) = mean(x(k:(k+interval)-1));
ymeans(s) = mean(y(k:(k+interval)-1));
end
s = s+interval
end

 採用された回答

Star Strider
Star Strider 2021 年 3 月 11 日

1 投票

When s >= 7 the code overwrites the array.
The indices are otherwise all integers greater than 0, so the problem with them not being integers doesn’t appear.

10 件のコメント

Robyn Seery
Robyn Seery 2021 年 3 月 11 日
Yes, I see that. So my question now is how do I return correct averages for [1,2,3], [4,5,6] and [7,8,9] for the x and y data.
This code fills both the xmeans and ymeans array with [5,0,0,8,0,0,8], and I am unsure as to why.
You need to set the appropriate limits on the subscripts.
However there may be easier ways to do what you want, specifically the movmean function (introduced in R2016a).
If you want to take only the means of specific blocks (for example blocks of 3) of the vectors, one way is:
x = [1;2;3;4;5;6;7;8;9];
y = [1;2;3;4;5;6;7;8;9];
xmeans = mean(reshape(x, 3, []));
ymeans = mean(reshape(y, 3, []));
producing:
xmeans =
2 5 8
ymeans =
2 5 8
.
Robyn Seery
Robyn Seery 2021 年 3 月 11 日
I think this will do the trick! I basically want to segment both axis for averaging, just like this!
Thank you very much for your help and quick responses.
Star Strider
Star Strider 2021 年 3 月 11 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
darova
darova 2021 年 3 月 12 日
Robyn Seery
Robyn Seery 2021 年 3 月 14 日
Hi, this should be marked as an accepted answer, but I am not being given the option to accept it for some reason!
Thank you for the help.
Star Strider
Star Strider 2021 年 3 月 14 日
As always, my pleasure!
I will notify MathWorks about the problem.
Rena Berman
Rena Berman 2021 年 3 月 15 日
編集済み: Rena Berman 2021 年 3 月 15 日
(Answers Dev) I have figured out the issue and emailed Robyn how to accept the answer.
Robyn Seery
Robyn Seery 2021 年 3 月 15 日
Accepted the answer. Thanks for the help everyone!
Star Strider
Star Strider 2021 年 3 月 15 日
Robyn Seery — Thank you!
Rena Berman — Thank you for helping with this!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by