Creating a Matrix with Ones at Each End and Zeros in Between in Matlab

In summary, the conversation is about creating a matrix with a specific form in Matlab. The person is trying to find a way to do this without manually typing everything in. They discuss two possible forms for the matrix and then come up with a solution using a for loop and the zeros and ones functions in Matlab.
  • #1
glyvin
4
0
Hi, I'm trying to write an SOR program in Matlab and have everything done, except I cannot figure out how to create a matrix of the following form without manually typing everything in:
x=(1,0,...,0,1)T for various sized matrices.

Thank you for your help.
 
Physics news on Phys.org
  • #2
What is x equal to? Is it x=(1,0,1,0,...,1,0) or x=(1,0,0,1,1,0,0,1,...,0,1)

In the first case, the expression "1,0" appears over and over again
In the second case, the expression "1,0,0,1" appears over and over again

Here is an idea for the first case:

n=10 <-- any number
x=zeros(n,1);
for i=1:n
if(mod(i,2)!=0)
x(i)=1;
endfor
 
  • #3
I think glyvin wants a set of zeros inside a a vector with only ones at each end.

This can be done fairly simply with something like
x=zeros(n,1);
x(1)=1; x(end)=1;
 

1. How do I create a matrix in Matlab?

To create a matrix in Matlab, you can use the matrix function and specify the number of rows and columns. For example, A = matrix(3,4) will create a matrix with 3 rows and 4 columns. Alternatively, you can also use the zeros or ones functions to create a matrix filled with zeros or ones, respectively.

2. Can I create a matrix with specific values in Matlab?

Yes, you can create a matrix with specific values by using the eye function to create an identity matrix, or the diag function to create a diagonal matrix. You can also use the rand or randn functions to create a matrix with random values.

3. How do I add or remove elements to a matrix in Matlab?

To add elements to a matrix, you can use the horzcat or vertcat functions to concatenate matrices horizontally or vertically, respectively. To remove elements, you can use the reshape function to resize the matrix, or the delete function to remove specific rows or columns.

4. How can I perform operations on matrices in Matlab?

To perform operations on matrices in Matlab, you can use vectorized operations such as +, -, *, and / to perform addition, subtraction, multiplication, and division, respectively. You can also use functions like transpose and inv to transpose or invert a matrix.

5. Can I save and load matrices in Matlab?

Yes, you can save a matrix in Matlab by using the save function and specifying the filename and variable name. To load a saved matrix, you can use the load function and specify the filename and variable name. You can also use the fprintf function to save a matrix in a text file, or the xlswrite function to save a matrix in an Excel file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
974
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top