Matlab, adding columns in matrices

In summary, the conversation is about creating a function in Scilab to add a row of zeros to the right of an nxn matrix, with the additional task of adding a row of 6's at the bottom. The individual has attempted to use the ones() and zeros() functions, but encountered errors and is seeking help on a relevant forum.
  • #1
slugbunny
15
0

Homework Statement



I need to create a function for scilab which adds a row of zeros to the right of an nxn matrix, a.
I also have to add a row of 6's at the bottom but I think i have that part down.

Homework Equations



I think ones(,) is supposed to help?

The Attempt at a Solution



I have tried to use ones...

function m_1=matrix_1(a,n)
m_1=[a,0*(ones(n;1))]
endfunction

I figure if [a;ones(1,n)] gives 1 row and n columns (at the bottom), then [a,ones(n;1)] should make n rows and 1 column. Instead I have an error 'waiting for right parenthesis.'

I have also thought of zeros but I don't know how to add n rows of zeros since you must type [0;0;0...] for each row.
 
Physics news on Phys.org
  • #2
Sorry, I realize there is a math & science software forum.
 
  • #3
u should do
m= [a zeros(1,size(m,2))];
 

1. How do I add a column to a matrix in Matlab?

To add a column to a matrix in Matlab, you can use the horzcat function. This function takes in two or more matrices and concatenates them horizontally, effectively adding a column to the original matrix. For example, if you have a matrix A and want to add a column b, you can use the command C = horzcat(A, b).

2. Can I add multiple columns to a matrix at once in Matlab?

Yes, you can add multiple columns to a matrix at once in Matlab using the horzcat function. Simply provide all the columns you want to add as input arguments, separated by commas. For example, if you want to add columns b, c, and d to the matrix A, you can use the command C = horzcat(A, b, c, d).

3. How do I add a column of zeros to a matrix in Matlab?

To add a column of zeros to a matrix in Matlab, you can use the zeros function to create a column vector of zeros, and then use the horzcat function to add it to the original matrix. For example, if you have a matrix A and want to add a column of zeros, you can use the command C = horzcat(A, zeros(size(A,1),1)).

4. Is there a way to add a column to a specific location in a matrix in Matlab?

Yes, you can use the insertrows function to add a column to a specific location in a matrix in Matlab. This function takes in the original matrix, the column to be inserted, and the index at which the column should be inserted. For example, if you have a matrix A and want to insert a column b at index 3, you can use the command C = insertrows(A, b, 3).

5. How can I add a column to a matrix without changing the original matrix in Matlab?

You can use the bsxfun function to add a column to a matrix without changing the original matrix in Matlab. This function applies a binary operation (such as addition or subtraction) between two arrays with singleton expansion. You can use it to concatenate a column vector to a matrix without changing the original matrix. For example, if you have a matrix A and want to add a column b without changing A, you can use the command C = bsxfun(@plus, A, b).

Similar threads

  • Calculus and Beyond Homework Help
Replies
2
Views
980
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
870
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
567
  • Calculus and Beyond Homework Help
Replies
7
Views
1K
  • Calculus and Beyond Homework Help
Replies
14
Views
589
  • Precalculus Mathematics Homework Help
Replies
1
Views
518
  • Calculus and Beyond Homework Help
Replies
1
Views
636
  • Calculus and Beyond Homework Help
Replies
3
Views
2K
  • Calculus and Beyond Homework Help
Replies
10
Views
980
Back
Top