Aligning then subtracting unequal length column vectors

4 vues (au cours des 30 derniers jours)
Jason
Jason le 25 Avr 2024
Commenté : Voss le 25 Avr 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 commentaires
Jason
Jason le 25 Avr 2024
Déplacé(e) : Dyuman Joshi le 25 Avr 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 le 25 Avr 2024
Déplacé(e) : Dyuman Joshi le 25 Avr 2024
I forgot to say, i need the 1st column values together with tge 3rd column (as i will be plotting these)

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 25 Avr 2024
Modifié(e) : Voss le 25 Avr 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>
  2 commentaires
Jason
Jason le 25 Avr 2024
thankyou thats perfect!
Voss
Voss le 25 Avr 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by