Inserting Zeros in a Matrix

Hi
Is there a way of inserting zeros in a data-matrix of 3x1(say). Only thing the zeros need to be inserted in those positions outside the data-matrix. So you would get a 10x1 matrix of zeros and datavalues.
For example,
DataCol = [x1;x2;x3] % Data column
NewDataCol = [0;0;0;0;0;x1;x2;x3;0;0] % New Data Column
The length of the DataCol changes each time.
Is there an easy way to insert the necessary zeros depending on the length of the data column?
Thanks
Ferd

3 Kommentare

Daniel Shub
Daniel Shub am 20 Mär. 2012
You need to give more details. Do you always start with Nx1 and want to end up with Mx1 with M >= N? Assuming M = N+k, how do you distribute the k zeros (how many zeros go above and how many go below)?
Oleg Komarov
Oleg Komarov am 20 Mär. 2012
Yes, but you have to tell us on which position to start inserting DataCol each time into the colum of zeros.
Or phrased differently, what's the rule that decides how many zeros above and how many below.
Ferd
Ferd am 20 Mär. 2012
@Daniel - Hey! yea, I always start with a Nx1 data-matrix and the Mx1 is my universal matrix. I have a data-matrix(Nx1 = MainTiming channel in degrees from -12 to +10 TDC) 160x1 matrix long. And I also have a Universal matrix (Mx1 = Crank Angle Degrees from -180 to 180) 360x1 matrix long. I need to bring this data-matrix into a 360x1 matrix by inserting zeros outside these data values so that I can plot the MainTiming on a universal scale from -180 to +180 for ease.
@Oleg - Hey! the position to begin inserting 0's would be dependent on the data-matrix channel (in this case MainTiming in degrees).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Dr. Seis
Dr. Seis am 20 Mär. 2012

1 Stimme

preZeros = 5;
postZeros = 2;
DataCol = rand(3,1);
%if really column vector
NewDataCol = zeros(length(DataCol)+preZeros+postZeros,1);
NewDataCol(preZeros+(1:length(DataCol)),1) = DataCol;

1 Kommentar

Ferd
Ferd am 20 Mär. 2012
Hey Kewl thanks! This worked! I modified the preZeros and postZeros variable w.r.t the lengths of the parameters. Thanks again!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Daniel Shub
Daniel Shub am 20 Mär. 2012

1 Stimme

Based on the additional information in your comment, you have x (160x1) and you want a function that will take in some offset b (0 <= b <= 200) and x and then create a matrix y (360x1) such that x(i) = y(i+b) for 1 <= i <= 160?
Without any error checking
f = @(b, x)([zeros(b, 1); x; zeros(200-b, 1)]);
NewDataCol = f(0, DataCol);
NewDataCol = f(200, DataCol);

4 Kommentare

Ferd
Ferd am 20 Mär. 2012
Hey Daniel,
Technically, this works too based on how I questioned (my bad I didn't convey correctly). It does the question justice. The result is the first 200rows as 0's and the remainder is substituted with the DataCol. But then I would also need to have 0's post the datavalues. Ideally, if I plot these, it will give me the same output, but visually I would like to see the values in the form of 0's-Datavalues-0's matrix.
None-the-less, I appreciate the reply!
Thanks!
Daniel Shub
Daniel Shub am 20 Mär. 2012
I think that is what it does. I showed the edge conditions f(0, DataCol) which will have 0 preceding zeros and 200 following zeros and f(200, DataCol) which will have 200 preceding zeros and 0 following zeros. In general f(n, DataCol) will have n leading zeros and 200-n lagging zeros.
Daniel Shub
Daniel Shub am 20 Mär. 2012
Also, the best way to show appreciation on the answers is by up voting.
Ferd
Ferd am 20 Mär. 2012
Yup!
oh! will do... Didn't know about the voting thing. I am new to this Matlab environment.

Melden Sie sich an, um zu kommentieren.

Geoff
Geoff am 20 Mär. 2012

0 Stimmen

How about this:
startRow = 6;
NewDataCol = zeros(10,1);
NewDataCol(startRow-1 + (1:numel(DataCol))) = DataCol;

Gefragt:

am 20 Mär. 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by