Clear Filters
Clear Filters

Calculate average for 0.01 step in first column

2 views (last 30 days)
Stefht
Stefht on 27 Sep 2017
Edited: dpb on 28 Sep 2017
Dear guys, I try a lot of time to do this but till now I didn t have success. I have a matrix A in which I want calculate for step of 0.01 for column x the average corresponding to value in z.I want to obtain a new matrix like showed in the picture:

Answers (2)

dpb
dpb on 27 Sep 2017
Edited: dpb on 28 Sep 2017
I'd already posted this once but there was no Answer connected so I'll try again...
Presuming no missing data, use the native memory storage order by column to your advantage--
z=mean(reshape(A(:,3),10,[])); % reshape by 10 rows for M columns and average
B=[A(1:10:end,1) z.']; % build output array with wanted x and computed z vectors
You can build the final array in place without needing the temporary intermediate variable z, of course; easier to read without the extra layers of parentheses for tutorial purposes here.
  1 Comment
Jan
Jan on 27 Sep 2017
Edited: Jan on 27 Sep 2017
+1.
I'd already posted this once but there was no Answer connected
27-Sep-2017 15:12 UTC, I have strange problems with the forum's interface also. Some comments cannot be sent, because the contents of the page has changed. Or a reload of a page fails with a message concerning a too high server load.

Sign in to comment.


Jan
Jan on 27 Sep 2017
You did not specify, if the data are dense. Do you have 10 measurements for all intervals? What is an efficient solution, if the number of measurements is not the same for each bin?
[n, edge, bin] = histcounts(x, 100:0.1:x(end));
result = splitapply(@mean, z, bin);
But I hesitate to trust 100:0.1:x(end) due to the old effect: (0:0.1:1) == 0.3. Perhaps we should convert the indexing to integer values:
[n, edge, bin] = histcounts(x*10, 1000:x(end)*10);
But I'm not really happy, because this relies on a hard coded factor.

Products

Community Treasure Hunt

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

Start Hunting!