Merging two arrays's

1 回表示 (過去 30 日間)
Joseph Buckley
Joseph Buckley 2016 年 11 月 28 日
編集済み: Sean de Wolski 2016 年 11 月 28 日
Hi,
So I have two arrays, say:
A={[1,2,3];[4,5];[]}
B={[6];[7,8,9];[10]}
and I would like to merge them to give:
C={[1,2,3,6];[4,5,7,8,9];[10]}
Is there a nice vectorised way to do this, or do I need to resort to for loops? (I will be calling this function >100000 times so quicker the better)/
Ideally I would like to ensure no value appears twice in the same row, i.e.: {[1,2,1];...} would not be allowed, however {[1,2];[1];..} would be allowed. However I could miss this part out if it would significantly slow down the code.

採用された回答

Sean de Wolski
Sean de Wolski 2016 年 11 月 28 日
編集済み: Sean de Wolski 2016 年 11 月 28 日
cellfun(@horzcat,A,B,'UniformOutput',false)
If you want to ensure uniqueness, wrap a unique around the horzcat
cellfun(@(x,y)unique([x,y]),A,B,'UniformOutput',false)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by