# how to extract n rows in a matrix column iteratively?

2 visualizzazioni (ultimi 30 giorni)
Kim Arnold il 3 Mar 2020
Commentato: Kim Arnold il 4 Mar 2020
i have a matrix A(15,1) looking like this
0.4218
0.9157
0.7922
0.9595
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
0.7431
0.3922
0.6555
0.1712
0.7060
I want to extract always 3 rows iteratively and put it in a cell,
In the first cell i want to have
0.4218
0.9157
0.7922
in the second cell i want to have
0.9595
0.6557
0.0357
and so on..., i would then end up with 5 cells as this {1,5}(3,1)
is there an easy way to do it without writing several for loops?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

James Tursa il 3 Mar 2020
Modificato: James Tursa il 3 Mar 2020
Depending on what you are doing downstream in your code, it may make more sense to simply reshape and then access the columns. E.g.,
B = reshape(A,3,[]);
then in your code use B(:,k) to access the k'th column
But if you really want/need the cell array approach then maybe something like this would suffice
B = mat2cell(A,3*ones(1,numel(A)/3));
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Kim Arnold il 4 Mar 2020
Thanks a lot, gives me exactly what i need!

Accedi per commentare.

### Più risposte (1)

Andrei Bobrov il 3 Mar 2020
Modificato: Andrei Bobrov il 3 Mar 2020
out = num2cell(reshape(A,3,[]),1);
or
out = accumarray(ceil((1:numel(A))'/3),A,[],@(x){x});
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Kim Arnold il 4 Mar 2020

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing 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