Creating average of y-values for similar x-values in dataset

조회 수: 15 (최근 30일)
Aundrea Dolan
Aundrea Dolan 2016년 3월 8일
답변: Guillaume 2016년 3월 8일
Hello,
I have a data set of x,y values where x identifies a line value and y identified extracted values from points along that line (from a GIS raster layer). I am new to Matlab and am struggling to create a working for loop that allows me to calculate the averages of the y point values for each x line value. (So, for line 1- calculate the average of all points along that line, for over 20,000 lines).
Does anyone know how to get this started? My attempts so far have failed, this is my most recent:
for y = 1:col
if x == 1
output = mean(y);
end
end
But this is not working correctly. Any ideas?
Thank you!

답변 (1개)

Guillaume
Guillaume 2016년 3월 8일
I think you need to spend more time learning matlab as your attempt does not even qualify as a half attempt. There's a complete disconnect between what you're asking and what your loop is doing.
Anyway, you don't need a loop for that, just two functions: unique and accumarray:
[lines, ~, subs] = unique(x);
ymeanperline = accumarray(subs, y, [], @mean);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by