Add Column Data to Existing Data Array at Specific Locations MATLAB

Hi - I have a matrix of data that is A = [ 1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4; 5 5 5 5] and B = [ 6 6; 7 7; 8 8; 9 9; 10 10] and an index ind = [ 1 0 0 0 1 0 ] that changes where the insertion point or "1" is located in the column order in other arrays. I need to insert the data of B into A but in the place of index == 1. So, the final matrix needs to look like C = [ 6 1 1 1 6 1; 7 2 2 2 7 2; 8 3 3 3 8 3; 9 4 4 4 9 4; 10 5 5 5 10 5] or like this below. I've searched the web but haven't seen the unique answer and I cannot seem to develop a trick to accomplish this...Thank you!
A looks like this
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
B looks like this
6 6
7 7
8 8
9 9
10 10
C final looks like this (based on the index "ind"
6 1 1 1 6 1
7 2 2 2 7 2
8 3 3 3 8 3
9 4 4 4 9 4
10 5 5 5 10 5

2 件のコメント

Star Strider
Star Strider 2017 年 12 月 21 日
Is this homework?
Randall
Randall 2017 年 12 月 21 日
No, I'm not a student but a meteorologist and not a code writer working under tight deadlines:)

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

 採用された回答

James Tursa
James Tursa 2017 年 12 月 21 日
編集済み: James Tursa 2017 年 12 月 21 日

1 投票

Going on faith that this is not homework, e.g., assuming that your ind vector has the correct number of 0's and 1's to account for the total columns of A and B:
result = zeros(size(A,1),numel(ind));
gind = logical(ind);
result(:,gind) = B;
result(:,~gind) = A;

4 件のコメント

Randall
Randall 2017 年 12 月 21 日
excellent, I understand each step - thank you. I work for a renewable energy company in Portland, Oregon, USA.
James Tursa
James Tursa 2017 年 12 月 21 日
Rip City!!!
Randall
Randall 2017 年 12 月 21 日

indeed. unfortunately, we're in the West with Warriors and Rockets and Spurs but we're used to this. thanks again,

Shang Liu
Shang Liu 2018 年 3 月 8 日

And now it's a very competitive team again!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2017 年 12 月 21 日

コメント済み:

2017 年 12 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by