Aligning then subtracting unequal length column vectors

조회 수: 3 (최근 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>

추가 답변 (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