Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
for loop matrix

Subject: for loop matrix

From: Jong Hoong

Date: 10 Mar, 2011 04:48:05

Message: 1 of 6

Hi all,
how would i create a for loop matrix like this:

A = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
    0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
     0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
     0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0;...
     0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0;...
     0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0;...
     0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0;...
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0;...
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0;...
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1];

size(A)= [10 20]
I tried this where my m=2, n= 20:

A = [ones(1,m) zeros(1,n-m);...
    zeros(1,m) ones(1,m) zeros(1,n-2*m);...
    zeros(1,2*m) ones(1,m) zeros(1,n-3*m);...
    zeros(1,3*m) ones(1,m) zeros(1,n-4*m);...
    zeros(1,4*m) ones(1,m) zeros(1,n-5*m);...
    zeros(1,5*m) ones(1,m) zeros(1,n-6*m);...
    zeros(1,6*m) ones(1,m) zeros(1,n-7*m);...
    zeros(1,7*m) ones(1,m) zeros(1,n-8*m);...
    zeros(1,8*m) ones(1,m) zeros(1,n-9*m);...
    zeros(1,9*m) ones(1,m)];

and now, how would I use for loop to create the matrix if i wan my
size(A) to be flexible so that I can create larger matrix?

Subject: for loop matrix

From: Paulo Silva

Date: 10 Mar, 2011 05:09:05

Message: 2 of 6

"Jong Hoong " <jonghoong@hotmail.com> wrote in message <il9l65$7ao$1@fred.mathworks.com>...
> Hi all,
> how would i create a for loop matrix like this:
>
> A = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1];
>
> size(A)= [10 20]
> I tried this where my m=2, n= 20:
>
> A = [ones(1,m) zeros(1,n-m);...
> zeros(1,m) ones(1,m) zeros(1,n-2*m);...
> zeros(1,2*m) ones(1,m) zeros(1,n-3*m);...
> zeros(1,3*m) ones(1,m) zeros(1,n-4*m);...
> zeros(1,4*m) ones(1,m) zeros(1,n-5*m);...
> zeros(1,5*m) ones(1,m) zeros(1,n-6*m);...
> zeros(1,6*m) ones(1,m) zeros(1,n-7*m);...
> zeros(1,7*m) ones(1,m) zeros(1,n-8*m);...
> zeros(1,8*m) ones(1,m) zeros(1,n-9*m);...
> zeros(1,9*m) ones(1,m)];
>
> and now, how would I use for loop to create the matrix if i wan my
> size(A) to be flexible so that I can create larger matrix?

a=zeros(10,20);
for l=1:size(a,1)
a(l,2*l-1)=1;
a(l,2*l)=1;
end

Subject: for loop matrix

From: Paulo Silva

Date: 10 Mar, 2011 05:16:04

Message: 3 of 6

a=zeros(10,20);
for l=1:size(a,1),a(l,2*l-1:2*l)=1;end

Subject: for loop matrix

From: Jong Hoong

Date: 10 Mar, 2011 05:45:20

Message: 4 of 6

"Paulo Silva" wrote in message <il9mqk$nn7$1@fred.mathworks.com>...
> a=zeros(10,20);
> for l=1:size(a,1),a(l,2*l-1:2*l)=1;end

thanks for your answer but with this, the matrix can only be create with 2ones.
how would i creating 3ones or more if my matrix dimension has to be input differently by users? for instance, if user input m= 2 (ones), n = 11, then

a = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
       0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
       0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
       0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
       0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0;...
       0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0;...
       0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0;...
       0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0;...
       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0;...
       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0;...
       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1];

size(a) = [11 22]

Subject: for loop matrix

From: Roger Stafford

Date: 10 Mar, 2011 06:18:05

Message: 5 of 6

"Jong Hoong " <jonghoong@hotmail.com> wrote in message <il9l65$7ao$1@fred.mathworks.com>...
> Hi all,
> how would i create a for loop matrix like this:
>
> A = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0;...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1];
> .........
- - - - - - - - -
  See this 9 Apr 2010 thread:

 http://www.mathworks.com/matlabcentral/newsreader/view_thread/278891

The same question was posed there.

Roger Stafford

Subject: for loop matrix

From: Roger Stafford

Date: 10 Mar, 2011 06:44:05

Message: 6 of 6

"Roger Stafford" wrote in message <il9qes$lsa$1@fred.mathworks.com>...
> See this 9 Apr 2010 thread:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/278891
>
> The same question was posed there.
>
> Roger Stafford
- - - - - - - - -
  Generalizing on Bruno's solution in that 9 Apr 2010 thread:

 a = kron(eye(n),ones(1,m));

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us