Thread Subject: Insert zeros into a vector

Subject: Insert zeros into a vector

From: Diego Lass

Date: 24 Jun, 2009 23:20:17

Message: 1 of 3

Hi,
I want to insert zeros into a vector in various positions. But not necessarily every other elements. For example
A = [ 1; 3; 4 ;5 ;6;7]
A =
     1
     3
     4
     5
     6
     7
I want to insert at the every 3 rd positon
     1
     3
     0
     4
     5
     0
     6
     7
     0
What is the most efficient way of doing this?
Thanks.
Diego

 

Subject: Insert zeros into a vector

From: Nathan

Date: 24 Jun, 2009 23:34:02

Message: 2 of 3

On Jun 24, 4:20 pm, "Diego Lass" <dlISC...@gmail.com> wrote:
> Hi,
> I want to insert zeros into a vector in various positions.  But not necessarily every other elements.  For example
> A = [ 1; 3; 4 ;5 ;6;7]
> A =
>      1
>      3
>      4
>      5
>      6
>      7
> I want to insert at the every 3 rd positon
>      1
>      3
>      0
>      4
>      5
>      0
>      6
>      7
>      0
> What is the most efficient way of doing this?
> Thanks.
> Diego

http://www.mathworks.ch/matlabcentral/newsreader/view_thread/156659

% the data
     v=1:7;
     w=[0,0,0];
     pos=[3,6,9];
% the engine
     tf=false(1,numel(v)+numel(w));
     r=double(tf);
     tf(pos)=true;
     r(tf)=w;
     r(~tf)=v;
% the result
     r

Subject: Insert zeros into a vector

From: Diego Lass

Date: 24 Jun, 2009 23:41:01

Message: 3 of 3

Got it!

>> B = reshape(A, 2, 6/2)

B =

     1 4 6
     3 5 7

>> B = cat(1,B, sparse(1,6/2))

B =

   (1,1) 1
   (2,1) 3
   (1,2) 4
   (2,2) 5
   (1,3) 6
   (2,3) 7

>> B = reshape(B, [], 1 )

B =

   (1,1) 1
   (2,1) 3
   (4,1) 4
   (5,1) 5
   (7,1) 6
   (8,1) 7

>> full(B)

ans =

     1
     3
     0
     4
     5
     0
     6
     7
     0



"Diego Lass" <dlISCool@gmail.com> wrote in message <h1ucbh$f5i$1@fred.mathworks.com>...
> Hi,
> I want to insert zeros into a vector in various positions. But not necessarily every other elements. For example
> A = [ 1; 3; 4 ;5 ;6;7]
> A =
> 1
> 3
> 4
> 5
> 6
> 7
> I want to insert at the every 3 rd positon
> 1
> 3
> 0
> 4
> 5
> 0
> 6
> 7
> 0
> What is the most efficient way of doing this?
> Thanks.
> Diego
>
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix operation Diego Lass 24 Jun, 2009 19:24:03
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com