# how to divide data array into a number of blocks like this using a For Loop?

8 visualizzazioni (ultimi 30 giorni)
Ved il 17 Nov 2013
Risposto: Image Analyst il 17 Nov 2013
N=16;
data=[1:N] % original data array 1xN
nb=[1,2,4,8] % number of data blocks
output: I want to divide my data (1x16) into different blocks=1,2,4,8 like this:
% output blocks as nob1,nob2,nob4,nob8
% nob1 is as it is (1x16)
% All output blocks have same columns (N=16)
i.e.,
nob1=[1:16] % no division of data
nob2=[1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 9 10 11 12 13 14 15 16]
nob4=[1 2 3 4 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 5 6 7 8 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 9 10 11 12 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 13 14 15 16]
nob8=[1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 4 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 7 8 0 0 0 0 0 0 0 0
....
....
0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 16]
And also the generalized script or function for any value of N (for e.g., N=256 or N=1024)
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Azzi Abdelmalek il 17 Nov 2013
Modificato: Azzi Abdelmalek il 17 Nov 2013
N=16
nb=[1 2 4 8]
data=1:N
for k=1:numel(nb)
ii=nb(k);
v=zeros(ii,N)
for p=1:size(v,1)
idx=(p-1)*N/ii+1:p*N/ii;
v(p,idx)=data(idx);
end
out{k}=v;
end
celldisp(out)
##### 1 CommentoMostra NessunoNascondi Nessuno
Ved il 17 Nov 2013
@Azzi: Perfect !

Accedi per commentare.

### Più risposte (1)

Image Analyst il 17 Nov 2013
See the FAQ which addresses this very very common question: http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by