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:
compose matrix

Subject: compose matrix

From: eziwarman

Date: 20 Jul, 2013 00:44:11

Message: 1 of 6

hi guys,
I am having a bit fuzzy to loop the matrix

say you have the matrix (1x4)
u=[0 -5 0 5 0]
 and construct the matrix into (4x10)
y=[0 -5 0 5 0 0 0 0 0 0;...
     0 0 0 -5 0 5 0 0 0 0;...
     0 0 0 0 0 -5 0 5 0 0;...
     0 0 0 0 0 0 0 -5 0 5];

Thanks

Subject: compose matrix

From: Nasser M. Abbasi

Date: 20 Jul, 2013 02:05:34

Message: 2 of 6

On 7/19/2013 7:44 PM, eziwarman wrote:
> hi guys,
> I am having a bit fuzzy to loop the matrix
>
> say you have the matrix (1x4)
> u=[0 -5 0 5 0]
> and construct the matrix into (4x10)
> y=[0 -5 0 5 0 0 0 0 0 0;...
> 0 0 0 -5 0 5 0 0 0 0;...
> 0 0 0 0 0 -5 0 5 0 0;...
> 0 0 0 0 0 0 0 -5 0 5];
>
> Thanks
>

why not make a simple loop and do in the loop?
you have one row vector, and want to make matrix of
4 rows. Simply preallocate the matrix, and make
a for loop and stick each row in the right location.

Give it a try, and if stuck, will help.

I do not know if there is a direct function in Matlab, since
blkdiags() does not support an offeset and hence will not
do what you want.

--Nasser

Subject: compose matrix

From: eziwarman

Date: 20 Jul, 2013 23:18:11

Message: 3 of 6

"Nasser M. Abbasi" wrote in message <kscr9e$ks2$1@speranza.aioe.org>...
> On 7/19/2013 7:44 PM, eziwarman wrote:
> > hi guys,
> > I am having a bit fuzzy to loop the matrix
> >
> > say you have the matrix (1x4)
> > u=[0 -5 0 5 0]
> > and construct the matrix into (4x10)
> > y=[0 -5 0 5 0 0 0 0 0 0;...
> > 0 0 0 -5 0 5 0 0 0 0;...
> > 0 0 0 0 0 -5 0 5 0 0;...
> > 0 0 0 0 0 0 0 -5 0 5];
> >
> > Thanks
> >
>
> why not make a simple loop and do in the loop?
> you have one row vector, and want to make matrix of
> 4 rows. Simply preallocate the matrix, and make
> a for loop and stick each row in the right location.
>
> Give it a try, and if stuck, will help.
>
> I do not know if there is a direct function in Matlab, since
> blkdiags() does not support an offeset and hence will not
> do what you want.
>
> --Nasser
>
Thanks
could you give me a sample
i am a new learner with matlab

Thanks

Subject: compose matrix

From: Nasser M. Abbasi

Date: 21 Jul, 2013 00:33:04

Message: 4 of 6

On 7/20/2013 6:18 PM, eziwarman wrote:


>>> hi guys,
>>> I am having a bit fuzzy to loop the matrix
>>>
>>> say you have the matrix (1x4)
>>> u=[0 -5 0 5 0]
>>> and construct the matrix into (4x10)
>>> y=[ 0 -5 0 5 0 0 0 0 0 0;...
>>> 0 0 0 -5 0 5 0 0 0 0;...
>>> 0 0 0 0 0 -5 0 5 0 0;...
>>> 0 0 0 0 0 0 0 -5 0 5];
>>>
>>> Thanks

> could you give me a sample
> i am a new learner with matlab
>

something like: (btw, the final matrix shuld be 4x11
not 4x10)

-------------------------
u=[0 -5 0 5 0];
M=4;
N=11;
A=zeros(M,N);
offset = 1;
for i=1:M
     A(i,offset:offset+length(u)-1)=u;
     offset = offset+2;
end
-----------------

A =
      0 -5 0 5 0 0 0 0 0 0 0
      0 0 0 -5 0 5 0 0 0 0 0
      0 0 0 0 0 -5 0 5 0 0 0
      0 0 0 0 0 0 0 -5 0 5 0

Subject: compose matrix

From: eziwarman

Date: 26 Jul, 2013 21:54:07

Message: 5 of 6

"Nasser M. Abbasi" wrote in message <ksfa80$ug6$1@speranza.aioe.org>...
> On 7/20/2013 6:18 PM, eziwarman wrote:
>
>
> >>> hi guys,
> >>> I am having a bit fuzzy to loop the matrix
> >>>
> >>> say you have the matrix (1x4)
> >>> u=[0 -5 0 5 0]
> >>> and construct the matrix into (4x10)
> >>> y=[ 0 -5 0 5 0 0 0 0 0 0;...
> >>> 0 0 0 -5 0 5 0 0 0 0;...
> >>> 0 0 0 0 0 -5 0 5 0 0;...
> >>> 0 0 0 0 0 0 0 -5 0 5];
> >>>
> >>> Thanks
>
> > could you give me a sample
> > i am a new learner with matlab
> >
>
> something like: (btw, the final matrix shuld be 4x11
> not 4x10)
>
> -------------------------
> u=[0 -5 0 5 0];
> M=4;
> N=11;
> A=zeros(M,N);
> offset = 1;
> for i=1:M
> A(i,offset:offset+length(u)-1)=u;
> offset = offset+2;
> end
> -----------------
>
> A =
> 0 -5 0 5 0 0 0 0 0 0 0
> 0 0 0 -5 0 5 0 0 0 0 0
> 0 0 0 0 0 -5 0 5 0 0 0
> 0 0 0 0 0 0 0 -5 0 5 0

Thanks Nasser

Ezi

Subject: compose matrix

From: Roger Stafford

Date: 27 Jul, 2013 07:48:11

Message: 6 of 6

"eziwarman" wrote in message <kscmgr$enp$1@newscl01ah.mathworks.com>...
> say you have the matrix (1x4)
> u=[0 -5 0 5 0]
> and construct the matrix into (4x10)
> y=[0 -5 0 5 0 0 0 0 0 0;...
> 0 0 0 -5 0 5 0 0 0 0;...
> 0 0 0 0 0 -5 0 5 0 0;...
> 0 0 0 0 0 0 0 -5 0 5];
- - - - - - - - - -
  This can be done in one line. Let u be the given row vector, r be the number of desired rows in y, and f be the offset between rows. In your example you have u = [0,-5,0,5], r = 4, and f = 2.

 y = reshape([u';repmat([zeros(f*r,1);u'],r-1,1)],[],r)';

Roger Stafford

Tags for this Thread

No tags are associated with 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