Filter löschen
Filter löschen

Aligning then subtracting unequal length column vectors

4 Ansichten (letzte 30 Tage)
Jason
Jason am 25 Apr. 2024
Kommentiert: Voss am 25 Apr. 2024
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 Kommentare
Jason
Jason am 25 Apr. 2024
Verschoben: Dyuman Joshi am 25 Apr. 2024
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 am 25 Apr. 2024
Verschoben: Dyuman Joshi am 25 Apr. 2024
I forgot to say, i need the 1st column values together with tge 3rd column (as i will be plotting these)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 25 Apr. 2024
Bearbeitet: Voss am 25 Apr. 2024
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>

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by