Inserting Zeros in a Matrix
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Oleg Komarov
le 20 Mar 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.
Réponse acceptée
Dr. Seis
le 20 Mar 2012
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;
Plus de réponses (2)
Daniel Shub
le 20 Mar 2012
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 commentaires
Geoff
le 20 Mar 2012
How about this:
startRow = 6;
NewDataCol = zeros(10,1);
NewDataCol(startRow-1 + (1:numel(DataCol))) = DataCol;
0 commentaires
Voir également
Catégories
En savoir plus sur Expression Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!