Reputation: 387
I have a matrix with two columns, one of which is the date and another of which is a quantity that I have to perform some operations on. I wanted to add a third column to my existing matrix. I was going to go about this by formulating the third column as a column vector, and then adding it on to my existing matrix (although I am not sure how to add another column onto a matrix either- any help would be much appreciated!)
For my third column, I wanted to divide 399 by the (180,2) element in my existing matrix, and then each element in my new matrix would be formed by multiplying the value in the second column of the existing matrix by this quantity. My code was:
a3=([:,a(:,2).*399/a(180,2)])
and my existing matrix is
apre=dlmread('filename.csv',',',1,0);
a=[apre(1:180,:)]
Upvotes: 2
Views: 7928
Reputation: 14851
I am not sure how to add another column onto a matrix
How to add a column to an existing matrix ?
Example:
1 1 1
Mat = 1 1 1
1 1 1
3
Col = 3
3
Mat = [Mat, Col];
1 1 1 3
Mat = 1 1 1 3
1 1 1 3
apre=dlmread('filename.csv',',',1,0);
a=[apre(1:180,:)]
a is a matrix of size 180x2 iff filename.csv has 2 columns.
a = [a, a(:, 2).*399/(a(180, 2))];
Upvotes: 1