# How to fill the remaining "N" number of rows, of an already populated vector, with zeros

27 visualizzazioni (ultimi 30 giorni)
Austin Sowers il 18 Set 2018
Commentato: MF il 19 Set 2018
It may be confusing, so let me explain. The "GV" data I provided has values in it. It is size (18,001 x 1). I want the to fill the remaining "N" number of rows with zeros. So it may be a (20,000 x 1) or perhaps a (30,000 x 1) vector, but still with the first 18,001 rows filled with the "GV" values. How to do this??
Thank you in advance!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

MF il 18 Set 2018
Modificato: MF il 19 Set 2018
Here is an example. 1) Let's first make a random vector of the size you have specified and 2) fill the vector with zeros so that it reaches 20000 rows.
GV=rand(18001,1);
GV(end+1:20000)=0;
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Austin Sowers il 18 Set 2018
A very good point. Thank you guys for your help!
MF il 19 Set 2018
Thank you dpb. The code was modified. I apologise for any inconveniences.

Accedi per commentare.

### Più risposte (2)

Bruno Luong il 18 Set 2018
Modificato: Bruno Luong il 18 Set 2018
% ... Compute GV
GV(20000)=0 % if you are sure 20000>length(GV), if not protect with IF
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
dpb il 18 Set 2018
Good on ya', Bruno! :)

Accedi per commentare.

dpb il 18 Set 2018
Alternatively, and perhaps more efficient depending upon how GV is generated/obtained...
GV(Ntotal,1)=0; % preallocate (could also write zeros(Ntotal,1) )
GV(1:N)=GVcalculation(1:N); % put the calculation results in first N locations
Which way is more effective depends on the details we don't know...
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su MATLAB in Help Center e File Exchange

R2016b

### Community Treasure Hunt

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

Start Hunting!

Translated by