Combining XY plots with arbitrary line shapes (combining spectra)

8 vues (au cours des 30 derniers jours)
todd eliason
todd eliason le 11 Mai 2020
Modifié(e) : Guru Mohanty le 14 Mai 2020
I have two datasets (spectra) that are with respect to the same x y variables however the bounds/step size of x do not match. However the x values can overlap between the datasets. I want to create a curve that contains both of the datasets. Since the datasets are quite large this preculdes doing it manually, I included some general data below if that helps for the explanation.
x1 = [1, 2, 3, 4, 5, 6, 7]; y1 = [1, 4, 9, 16, 25, 36, 49]
x2 = [1.5, 3, 4.5, 6]; y2 = [10, 6, 4, 0]

Réponse acceptée

Guru Mohanty
Guru Mohanty le 14 Mai 2020
Modifié(e) : Guru Mohanty le 14 Mai 2020
Hi, I understand you are trying to plot common elements between two datasets. You can use intersect function to get an overlap between two datasets.Here is a sample code for it.
clc;clear all;
x1 = [1, 2, 3, 4, 5, 6, 7];
y1 = [1, 4, 9, 16, 25, 36, 49];
x2 = [1.5, 3, 4.5, 6];
y2 = [10, 6, 4, 0];
[common_x1x2,locx1,locx2 ] = intersect(x1,x2); % Common Element between x1 & x2
common_y1=y1(locx1); % Corresponding Element in y1
common_y2=y2(locx2); % Corresponding Element in y2
plot(common_x1x2,common_y1,common_x1x2,common_y2);
You Can also plot both the dataset.
clc;clear all;
x1 = [1, 2, 3, 4, 5, 6, 7];
y1 = [1, 4, 9, 16, 25, 36, 49];
x2 = [1.5, 3, 4.5, 6];
y2 = [10, 6, 4, 0];
diffx2x1=setdiff(x2,x1);
x1new=[x1,diffx2x1];
y1new=[y1,zeros(1,length(diffx2x1))];
diffx1x2=setdiff(x1,x2);
x2new=[x2,diffx1x2];
y2new=[y2,zeros(1,length(diffx1x2))];
plot(x1new,y1new,'o')
hold on
plot(x2new,y2new,'o')
hold off

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by