Averaging a curve with itself
5 views (last 30 days)
Show older comments
I'm trying to average this curve (pixel intensity of a selected object by distance from a point) with itself along the x-axis: 

I've tried linear interpolation, which was unsuccessful because I do not have fully unique values in x. However, my x-values also do not always match perfectly between the upper and lower curves, which means that I cannot just average y-coordinate by matching x-values.
What would be the best way to go about doing this? I have attached my code and the relevant .mat file in case that helps.
0 Comments
Accepted Answer
Star Strider
on 8 Sep 2023
Try this —
% type('membrane_intensity.m')
LD = load('test.mat');
x = LD.cx;
y = LD.c(:,1,1);
[xs,x1] = min(x);
[xe,x2] = max(x);
Lv = ismember(x, x(x1:x2));
Sec{1,:} = [x(Lv) y(Lv)]
Sec{2,:} = [x(~Lv) y(~Lv)]
Sec{2} = sortrows(Sec{2})
[Us1x,ia1,ic1] = unique(Sec{1}(:,1), 'stable');
Sec{1} = Sec{1}(ia1,:)
[Us2x,ia2,ic2] = unique(Sec{2}(:,1), 'stable');
Sec{2} = Sec{2}(ia2,:)
figure
plot(Sec{1}(:,1), Sec{1}(:,2))
hold on
plot(Sec{2}(:,1), Sec{2}(:,2))
hold off
title('Before Interpolation')
xv = linspace(xs, xe, 600).';
Seci{1,:} = [xv interp1(Sec{1}(:,1), Sec{1}(:,2), xv)]
Seci{2,:} = [xv interp1(Sec{2}(:,1), Sec{2}(:,2), xv)]
ymean = mean([Seci{1}(:,2) Seci{2}(:,2)], 2);
figure
plot(Seci{1}(:,1), Seci{1}(:,2), 'DisplayName','Section #1')
hold on
plot(Seci{2}(:,1), Seci{2}(:,2), 'DisplayName','Section #1')
plot(xv, ymean, '-g', 'DisplayName','Mean')
hold off
title('After Interpolation')
legend('Location','best')
.
1 Comment
Stephen23
on 8 Sep 2023
Moved: Stephen23
on 11 Sep 2023
Simpler without ISMEMBER and SORTROWS:
S = load('test.mat');
Xraw = S.cx;
Yraw = S.c;
[Xmin,Imin] = min(Xraw);
[Xmax,Imax] = max(Xraw);
I1 = Imin:Imax;
I2 = [Imax:numel(Xraw),1:Imin];
[X1,J1] = unique(Xraw(I1));
[X2,J2] = unique(Xraw(I2));
N = 654;
Xnew = linspace(Xmin,Xmax,N);
Ynew = mean([...
interp1(X1,Yraw(I1(J1)),Xnew);...
interp1(X2,Yraw(I2(J2)),Xnew)],1);
plot(Xraw,Yraw,Xnew,Ynew)
More Answers (0)
See Also
Categories
Find more on Detection in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

