Aligning then subtracting unequal length column vectors

4 ビュー (過去 30 日間)
Jason
Jason 2024 年 4 月 25 日
コメント済み: Voss 2024 年 4 月 25 日
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>
  2 件のコメント
Jason
Jason 2024 年 4 月 25 日
thankyou thats perfect!
Voss
Voss 2024 年 4 月 25 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by