How to replace portion of data set with another?

16 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2017 年 7 月 12 日
編集済み: dbmn 2017 年 7 月 12 日
Assume you have two 20x20 data sets (just an example). The first column of data contains the time the data was recorded (1,2,3,4,5) and the rest of the data are the values that correspond with that time. What I need to do is take the data from 5 to 10 seconds of one data set (assume this data set is a) and replace the 5 to 10 seconds of the second data set (across all columns, this data set is b), essentially giving me a merged data set.
I think that using indexing might be the best choice (and assuming our data sets are a and b), so something like
find(a(:,1)==5)
find(a(:,1)==10)
Those would give me the two locations of the data, but how would I remove the data from that data set and replace the data in the other?

採用された回答

Guillaume
Guillaume 2017 年 7 月 12 日
Assuming that both matrices have the same number of rows for your 5 to 10 range and that they're in the same order:
b(b(:, 1) >= 5 & b(:, 1) <= 10, 2:end) = a(a(:, 1) >= 5 & a(:, 1) <= 10, 2:end)
is one way of doing it.
  2 件のコメント
Ibro Tutic
Ibro Tutic 2017 年 7 月 12 日
This looks like what I need, however a quick question. Is this replacing the data in b with the data in a or vice versa? I think it's b getting replaced with a, but I want to make sure, thanks for the answer!
Guillaume
Guillaume 2017 年 7 月 12 日
Yes, b is the destination since it's the target of the =.

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

その他の回答 (1 件)

dbmn
dbmn 2017 年 7 月 12 日
One way to solve this would be by logical indexing (assuming both datasets have the same time increment)
% Example data
a=[1:10; rand(10,10)];
b=[1:10; ones(10,10)];
% Find time < 5s
index = a(1,:)<5
% Merge Data
b(:, index) = a(:,index);
  2 件のコメント
Ibro Tutic
Ibro Tutic 2017 年 7 月 12 日
Would this actually work in my case? Since the data goes from 1 to 20, I want to replace a portion of it, that occurs somewhere in the middle of the data set. In your case, it seems that you are only taking the data from 5 to 10 (because of the size of your matrix), and not accounting for the fact that more data could exist. Thoughts?
dbmn
dbmn 2017 年 7 月 12 日
編集済み: dbmn 2017 年 7 月 12 日
This should also work if the data occurs in the middle or even multiple patches at the same time f.ex.
% Find time 2 < t < 5
index = a(1,:)>2 & a(1,:)<5
what is important though is that the time variable in both datasets is the same...

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

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by