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 Comments

No, I'm not a student but a meteorologist and not a code writer working under tight deadlines:)

Sign in to comment.

 Accepted Answer

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 Comments

excellent, I understand each step - thank you. I work for a renewable energy company in Portland, Oregon, USA.

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

And now it's a very competitive team again!

Sign in to comment.

More Answers (0)

Asked:

on 21 Dec 2017

Commented:

on 21 Dec 2017

Community Treasure Hunt

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

Start Hunting!