Info

この質問は閉じられています。 編集または回答するには再度開いてください。

array operation for merging two arrays

1 回表示 (過去 30 日間)
sermet
sermet 2016 年 5 月 20 日
閉鎖済み: Azzi Abdelmalek 2016 年 5 月 20 日
data_1=[10;20;30;40];
data_2=[40;10];
data_3=[11;12];
for i=1:numel(data_2)
idx(i)=find(data_2(i)==data_1);
end
I need to merge data_2 and data_3 w.r.t idx array as follows;
merged_data=[10;11;12;40];
idx ([4 1]) determines the row number of 40 and 10 (data_2) within merged_data. Other row of the merged_data belongs to data_3.
data_2 and data_1 arrays always share at least 2 numbers. I need to merge data_2 and data_3 as an automatic way, keeping the order of rows w.r.t idx.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 20 日
sermet
sermet 2016 年 5 月 20 日
Azzi, please read my comment in the previous question.

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 20 日
編集済み: Andrei Bobrov 2016 年 5 月 20 日
union(intersect(data_1,data_2),data_3)
or
merge_data = data_1
merge_data(~ismember(merge_data,data_2)) = data_3
  1 件のコメント
sermet
sermet 2016 年 5 月 20 日
編集済み: sermet 2016 年 5 月 20 日
Andrei, it doesn't work all the time. Assume,
data_2=[20;30];
idx=[2 3];
Then, merged_data should be;
merged_data=[11;20;30;12];
but merged_data from your code;
merged_data=[11;12;20;30]; % it is ascending mode thus the order cannot be protected.
20 should be in the second row of the merged_data and 30 should be in the 3rd row of the merged_data w.r.t. idx array.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by