Aligning then subtracting unequal length column vectors

4 次查看(过去 30 天)
Hello, i have 2 vectors xspots and xgrid
xspots = [
2600.00
2679.00
2802.00
2924.00
3046.00
3169.00
3291.00
3413.00
3536.00
3658.00
3781.00];
xgrid = [
2680.00
2803.00
2926.00
3049.00
3172.00
3294.00
3417.00
3539.00];
Crucially they are not the ame length
what I want to do is locate in both, the nearest value to a reference (called ref) i.e. ref=3200.
I then want to align these two vectors such that the values closest to ref lign up (i.e. this will then be the values 3169 & 3172.)
Then I want the diff between xspots and xgrid in this aligned state.
This was my attempt, but its in 2 parts.
ref=3200;
mask = xgrid(:,1) < ref;
xgrid(mask,:) = [];
mask = xspots(:,1) < ref;
xspots(mask,:) = [];
n=min(numel(xspots,xgrid));
data=[];
for i=1:n
data(i,1)=xspots(i);
data(i,2)=xgrid(i);
end
data(:,3)=data(:,1)-data(:,2)
%% and then repeat this for > ref.... but it seems long winded.
  5 个评论
Jason
Jason 2024-4-25
移动:Dyuman Joshi 2024-4-25
This is the output I need. Notice how I align the two vectors at the ref. value, and then where there is overlap perform the subtraction
Jason
Jason 2024-4-25
移动:Dyuman Joshi 2024-4-25
I forgot to say, i need the 1st column values together with tge 3rd column (as i will be plotting these)

请先登录,再进行评论。

采纳的回答

Voss
Voss 2024-4-25
编辑:Voss 2024-4-25
xspots = [
2600.00
2679.00
2802.00
2924.00
3046.00
3169.00
3291.00
3413.00
3536.00
3658.00
3781.00];
xgrid = [
2680.00
2803.00
2926.00
3049.00
3172.00
3294.00
3417.00
3539.00];
ref=3200;
[~,idx1] = min(abs(xspots-ref));
[~,idx2] = min(abs(xgrid-ref));
n1 = numel(xspots);
n2 = numel(xgrid);
ii = intersect((1:n1)-idx1,(1:n2)-idx2);
difference = xspots(idx1+ii)-xgrid(idx2+ii)
difference = 8x1
-1 -1 -2 -3 -3 -3 -4 -3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result = [xspots(idx1+ii) difference]
result = 8x2
2679 -1 2802 -1 2924 -2 3046 -3 3169 -3 3291 -3 3413 -4 3536 -3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by