Creating a matrix with 1s at ends and 0s between in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter glyvin
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
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;