How do I horizontally concatenate the following 2 numerical arrays?

2 ビュー (過去 30 日間)
Brad
Brad 2013 年 11 月 27 日
回答済み: Andrei Bobrov 2013 年 11 月 27 日
I have 2 separate arrays that I'm trying to concatenate.
ND is a 2x6 double array as follows;
ND = 58560 NaN 58561 -1100 -5100 4100
58570 Nan 59571 -1200 -5200 4200
OD is a 3x4 double array as follows;
OD = 58564 1 58563 11
58572 2 58573 21
58572 2 58573 22
The desired output array is shown below (3x 10 double array):
output = 58560 NaN 58561 -1100 -5100 4100 58564 1 58563 11
58570 NaN 59571 -1200 -5200 4200 58572 2 58573 21
58570 NaN 59571 -1200 -5200 4200 58572 2 58573 22
Since the dimensions do not allow for either vertical or horizontal concatenation, is there a technique I could use to achieve the desired output?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 27 日
編集済み: Azzi Abdelmalek 2013 年 11 月 27 日
ND =[58560 nan 58561 -1100 -5100 4100;58570 nan 59571 -1200 -5200 4200]
OD =[58564 1 58563 11;58572 2 58573 21; 58572 2 58573 22]
n=size(ND,1);
m=size(OD,1);
ND(end:m,:)=repmat(ND(end,:),m-n+1,1)
out=[ND OD]
  1 件のコメント
Brad
Brad 2013 年 11 月 27 日
Azzi, thank you for looking at this. The answer you supplied works.
I do have a follow on question. In my original question, I supplied the sample arrays ND and OD, along with the desired output. In executing your answer I realized the sample matrices I supplied were too simplified. If the OD array is:
OD = 58564 1 58563 11
58564 1 58565 12
58572 2 58573 21
58572 2 58573 22
And the desired output is:
output = 58560 NaN 58561 -1100 -5100 4100 58564 1 58563 11
58560 NaN 58561 -1100 -5100 4100 58564 1 58565 12
58570 NaN 59571 -1200 -5200 4200 58572 2 58573 21
58570 NaN 59571 -1200 -5200 4200 58572 2 58573 22
How do I achieve the desired result when I can't use end as part of the repmat?

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 11 月 27 日
out = [ND(OD(:,2),:),OD]

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by