Daniel
Daniel

Reputation: 21

Replacing columns in an array

I have a 5x167 column array, with 167 rows example:

1 2 3 4 5, 
1 2 3 4 5, 
1 2 3 4 5, 
1 2 3 4 5, 
1 2 3 4 5, 
etc

I have another 1x167 column array of numbers, let's say 0's. I'd like to take this 1x167 column array and replace the middle (3rd) column with this array while deleting the 4th column so I get an array like this.

1 2 0 5,
1 2 0 5,
1 2 0 5,
1 2 0 5,
etc

How would I replace a whole column like this? I keep getting an array full of 0's

Upvotes: 0

Views: 44

Answers (1)

Wolfie
Wolfie

Reputation: 30165

Let's take some random data:

M = rand( 167, 5 ); % Your 5 column matrix
N = rand( 167, 1 ); % Your new column

You can delete the 4th column like so:

M(:,4) = [];        % Remove 4th column

Then replace the 3rd column

M(:,3) = N;         % Replace 3rd column of M with N

Alternatively, you can construct the matrix in one line

M = [ M(:,1:2), N, M(:,5) ]; % Construct new matrix by concatenating columns

Upvotes: 1

Related Questions