# How to create an average of 2 lines on a single graph

36 ビュー (過去 30 日間)
Charlie Hillary 2021 年 3 月 7 日
コメント済み: dpb 2021 年 3 月 8 日
Hi,
I have two lines which represent magnetic intensity data collected along a transect back and forth, hence the 2 lines, producing this figure;
% Single-line Magnetic Anomaly graph, Charlie Hillary 2021
% load top and bottom data files
dtopdt = (top(end,3)-top(1,3))/(top(end,2)-top(1,2)) ;
% correct the data
top(:,3) = top(:,3) - dtopdt*(top(:,2)-top(1,2)) ;
% plot
figure
plot(top(1:end-1,1),top(1:end-1,3)-48e3,'k-') ;
xlabel('Distance / m')
ylabel('Upper B / nT ') ;
title('Top @ 0.95m')
I need to create an average of the 2 lines. I have a rough understanding of how to do so. I think I need to create a loop for each Y value(s) at a given X value, and create an average of the two, by adding and dividing by two. The data is a 3 column array, with column 2 being used to process column 1 (x) and column 3 (y).
Just need a point in the right direction, thanks!
Charlie
##### 4 件のコメント表示非表示 3 件の古いコメント
Charlie Hillary 2021 年 3 月 7 日
Oh my bad you don't have the .txt file! Here it is. The -48000 is to remove the average background magnetic intensity for the location of the transect.
As you can see the data is also collected whilst returning from the end point in the transect generating 2 y values per given x value. I need these to me merged, but wasn't sure on how to do this with polyfit.
Charlie

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

### 採用された回答

dpb 2021 年 3 月 7 日
OK, I see...the data are collected on a up- and down-slope symmetric ramp..
Try something like--
top=reshape(top,length(top)/2,[]);
top(:,[2:2:end])=flipud(top(:,2:2:end));
top=array2table(top(:,[1 3:end]),'VariableNames',{'Distance','V1_Out','V1_In','B_Out','B_In'});
ans =
8×5 table
Distance V1_Out V1_In B_Out B_In
________ ______ ______ ________ ________
15.00 0.50 495.00 48700.67 48699.92
16.00 2.25 499.09 48701.12 48699.82
17.00 3.75 486.75 48700.58 48699.33
18.00 5.25 485.75 48701.99 48693.28
19.00 7.00 484.75 48702.83 48703.04
20.00 8.50 483.75 48703.13 48702.60
21.00 10.00 482.75 48703.30 48701.68
22.00 11.50 482.00 48703.92 48703.99
hAx(1)=subplot(2,1,1);
plot(top.Distance,[top.B_Out top.B_In])
legend('B Out','B In','Location','southwest')
xlim([0 265])
hAx(2)=subplot(2,1,2);
plot(top.Distance,detrend([top.B_Out top.B_In]))
xlim([0 265])
mnB=mean([top.B_Out top.B_In],'all')
>> mnB =
48685.42
>> mean([top.B_Out top.B_In])
ans =
48686.39 48684.44
>> diff(ans)
ans =
-1.95
>>
yyaxis right
hL=plot(top.Distance,[top.B_Out top.B_In]-mnB);
legend('B Out Detrended','B In Detrended','B Out Less Mean','B In Less Mean','Location','southwest')
This shows a very nearly flat profile going across as the small bias between the detrended and mean-subtracted signals show--what's the best thing to do I've no klew having no knowledge of what the data are (nor the field if did :) ).
Oh -- I realize I completely got carried away with the pretty figures and totally forget the actual Q? -- alhough by now it ought to be apparent how to do the mean of the two--
top.B_Avg=mean([top.B_Out top.B_In],2);
ans =
8×6 table
Distance V1_Out V1_In B_Out B_In B_Avg
________ ______ ______ ________ ________ ________
15.00 0.50 495.00 48700.67 48699.92 48700.29
16.00 2.25 499.09 48701.12 48699.82 48700.47
17.00 3.75 486.75 48700.58 48699.33 48699.96
18.00 5.25 485.75 48701.99 48693.28 48697.64
19.00 7.00 484.75 48702.83 48703.04 48702.94
20.00 8.50 483.75 48703.13 48702.60 48702.86
21.00 10.00 482.75 48703.30 48701.68 48702.49
22.00 11.50 482.00 48703.92 48703.99 48703.95
>>
Not knowing what to do with the second variable, I simply carried it along for the ride...
##### 2 件のコメント表示非表示 1 件の古いコメント
dpb 2021 年 3 月 8 日
Thanks for the background...interesting to know what one is looking at!
Why not remove the actual mean instead of something approximating it?
And, detrend does that along with compensating for a trend; not knowing the reason(s) behind any trend there might be; I already cleared the data but iirc the slope is pretty small; that's indicative in how well the two overlay when plot detrend(B) over B-mn(B).

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

### その他の回答 (1 件)

David Goodmanson 2021 年 3 月 8 日

Hi Charley
since the second half of the distance array has exactly the same values as the first half (only reversed), you can use just the first half of the distance array for plotting purposes. After you make the data correction to top3, then
dist = top(1:245,1);
B1 = top(1:245,3);
B2 = flip(top(246:end,3));
Bmean = mean([B1 B2],2);
plot(dist,B1,'b',dist,B2,'b',dist,Bmean,'r')
xlabel('Distance / m')
ylabel('Upper B / nT ') ;
title('Top @ 0.95m')
Here B1 and B2 are column vectors. The square brackets concatenate them side-by-side, creating a 245x2 matrix. The '2' in the mean command says to take the mean across rows, rather than down columns which is the default. So you get the mean of the two curves.
##### 1 件のコメント表示非表示 なし
Charlie Hillary 2021 年 3 月 8 日
Hi David,
This also worked well for me. I thought it would be difficult splitting the .txt file! Much simpler than I imagined, and an intuitive response so thanks.
Charlie

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

### Community Treasure Hunt

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

Start Hunting!

Translated by