Merge two data sets/matrices from different sources

6 ビュー (過去 30 日間)
Nimrod Sadeh
Nimrod Sadeh 2017 年 10 月 20 日
回答済み: Image Analyst 2017 年 10 月 21 日
Hi all,
I am trying to merge two CSV files. Each have two columns:
- Column 1 is an independent variable (numerical)
- Column 2 is dependent (numerical)
Each file contains a different dependent variable, so the final result should be a matrix with 3 columns: independent variable, and one column each for the dependent variable from each CSV file. However, since the data comes from different sources, the independent variable is sampled at different numerical value. I am OK with numerically interpolating one of the CSV files so that its dependent values correspond to the independent values from the other files, but I haven't figured out how to do this.

回答 (2 件)

dpb
dpb 2017 年 10 月 20 日
  1. Read file 1 -- call it X1
  2. Read file 2 -- call it X2
  3. Interpolate column 1 of X1 to match X2
X1=importdata('file1'); % read first file
X2=importdata('file2'); % and second
dat=interp1(X2(:,1),X2(:,2),X1(:,1)); % interpolate 2nd to match first time data
X=[X1 dat[; % put together in one array
That's arbitrarily using the first file times; alternatively you could compare the first/last and choose which has the longer range for the basis or whatever other criterion for picking one over the other you wished.
  2 件のコメント
Nimrod Sadeh
Nimrod Sadeh 2017 年 10 月 20 日
Thank you for your answer. It is a bit simpler than what would work here, as sometimes the independent data do not event overlap.
What I ended doing is combing the independent data, sorting, cleaning duplicates, and interpolating the dependent.
Walter Roberson
Walter Roberson 2017 年 10 月 21 日
Is there a natural interval for the independent interval?
Especially in the case of time units, sometimes easiest is to use timetables and synchronize

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


Image Analyst
Image Analyst 2017 年 10 月 21 日
You can use readtable() and innerjoin() or outerjoin(). You forgot to attach the files so that's about all I can suggest.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by