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 个评论

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

请先登录,再进行评论。

 采纳的回答

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 个评论

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!

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by