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

  • Thread starter Thread starter glyvin
  • Start date Start date
  • Tags Tags
    Matlab Matrices
AI Thread Summary
The discussion focuses on creating a specific matrix in Matlab, where the matrix has ones at each end and zeros in between. The user is unsure whether the desired format is alternating ones and zeros or just ones at the boundaries with zeros in between. A proposed solution involves initializing a zero matrix and setting the first and last elements to one. The conversation clarifies that the goal is to generate a matrix with only ones at the ends, simplifying the creation process. This method allows for easy adjustment of the matrix size without manual entry.
glyvin
Messages
4
Reaction score
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
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
 
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;
 
Back
Top