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

Is this homework?
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일
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에서 Matrix 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