PDA

View Full Version : Creating Matrices in Matlab


glyvin
Oct24-10, 11:55 AM
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.

MasterX
Oct26-10, 06:11 AM
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

cpt_carrot
Oct26-10, 08:32 AM
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;