Thread Subject: Inserting columns in a vector

Subject: Inserting columns in a vector

From: Dips Bhatia

Date: 26 Nov, 2009 09:58:04

Message: 1 of 7

Hi,
A basic question. Is there any command by which I can insert columns in a row vector such that the rest of the columns along with their data get shifted and the inserted columns have data values as 0.
Thanks a lot...

Subject: Inserting columns in a vector

From: Jos (10584)

Date: 26 Nov, 2009 10:51:03

Message: 2 of 7

"Dips Bhatia" <deepakdbhatia@gmail.com> wrote in message <heljfc$8km$1@fred.mathworks.com>...
> Hi,
> A basic question. Is there any command by which I can insert columns in a row vector such that the rest of the columns along with their data get shifted and the inserted columns have data values as 0.
> Thanks a lot...

Many approaches, inclusing my FEX file INSERTROWS

http://www.mathworks.com/matlabcentral/fileexchange/9984

A = magic(3)
B = insertrows(A.',0,2).'
% 8 1 0 6
% 3 5 0 7
% 4 9 0 2

hth
Jos

Subject: Inserting columns in a vector

From: Joe Nunes

Date: 26 Nov, 2009 10:54:04

Message: 3 of 7

"Dips Bhatia" <deepakdbhatia@gmail.com> wrote in message <heljfc$8km$1@fred.mathworks.com>...
> Hi,
> A basic question. Is there any command by which I can insert columns in a row vector such that the rest of the columns along with their data get shifted and the inserted columns have data values as 0.
> Thanks a lot...


Hello,
if you have this row vector, ee = [1 2 3 4 5 6 7 8 9]
you can simply and whatever you want with ee=[ee 0 0 0] % add 3 zeros in the end

or imagine if you wanted to add 1000 zeros in the end of ee.

ee = [ee zeros(1,1000)

Subject: Inserting columns in a vector

From: Lorenzo Guerrasio

Date: 26 Nov, 2009 11:12:03

Message: 4 of 7

I think I found this in a discussion. I hope is not published in FEX
function [V1]=push_in_vector(V,P,N);
%function [V1]=push_in_vector(V,P,N);
%push the value N in vector V after position P
V=V(:)';
L=size(V,2);
if P==1;
    V1=[V(1),N,V(2:end)];
elseif P==L-1;
    V1=[V(1:(L-1)),N,V(L)];
else
    V1=[diag(kron(V.',ones(1,P)));N;flipud(diag(kron(flipud(V.'),ones(1,length(V)-P))))];
end

"Dips Bhatia" <deepakdbhatia@gmail.com> wrote in message <heljfc$8km$1@fred.mathworks.com>...
> Hi,
> A basic question. Is there any command by which I can insert columns in a row vector such that the rest of the columns along with their data get shifted and the inserted columns have data values as 0.
> Thanks a lot...

Subject: Inserting columns in a vector

From: ttkingdom

Date: 26 Nov, 2009 11:21:27

Message: 5 of 7

I think there is a simple solution. For example if you have row = [1 2
3 4 5 6 7 8]

row = [row(1:3) 0 0 0 row(4:8)];
row = [1 2 3 0 0 0 4 5 6 7 8];

or if you don't know the size

row = [row(1:pos), zeros(1,num), row((pos + 1):size(row, 2))];

Hope this helps.

Subject: Inserting columns in a vector

From: Dips Bhatia

Date: 26 Nov, 2009 11:39:03

Message: 6 of 7

ttkingdom <ttkingdom@gmail.com> wrote in message <c43f90a1-420a-4645-8459-3363ed35233b@g1g2000pra.googlegroups.com>...
> I think there is a simple solution. For example if you have row = [1 2
> 3 4 5 6 7 8]
>
> row = [row(1:3) 0 0 0 row(4:8)];
> row = [1 2 3 0 0 0 4 5 6 7 8];
>
> or if you don't know the size
>
> row = [row(1:pos), zeros(1,num), row((pos + 1):size(row, 2))];
>
> Hope this helps.

Thank you very much for your response. I think this is very helpful for me..

Subject: Inserting columns in a vector

From: Dips Bhatia

Date: 26 Nov, 2009 11:40:21

Message: 7 of 7

Hi Lorenzo,
Thanks for your reply. I think this should help me.

"Lorenzo Guerrasio" <lorenzo.guerrasio@email.it> wrote in message <helnq3$9kv$1@fred.mathworks.com>...
> I think I found this in a discussion. I hope is not published in FEX
> function [V1]=push_in_vector(V,P,N);
> %function [V1]=push_in_vector(V,P,N);
> %push the value N in vector V after position P
> V=V(:)';
> L=size(V,2);
> if P==1;
> V1=[V(1),N,V(2:end)];
> elseif P==L-1;
> V1=[V(1:(L-1)),N,V(L)];
> else
> V1=[diag(kron(V.',ones(1,P)));N;flipud(diag(kron(flipud(V.'),ones(1,length(V)-P))))];
> end
>
> "Dips Bhatia" <deepakdbhatia@gmail.com> wrote in message <heljfc$8km$1@fred.mathworks.com>...
> > Hi,
> > A basic question. Is there any command by which I can insert columns in a row vector such that the rest of the columns along with their data get shifted and the inserted columns have data values as 0.
> > Thanks a lot...

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
push in vector Lorenzo Guerrasio 26 Nov, 2009 06:14:04
inserting columns Dips Bhatia 26 Nov, 2009 04:59:07
rssFeed for this Thread

Contact us at files@mathworks.com