How to extract matrix with different starting index for each row?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Katherine Liu
il 20 Ott 2020
Commentato: Katherine Liu
il 20 Ott 2020
I am not sure how to extract from a matrix a smaller matrix with different starting index for each row.
For example,
Original matrix (4x5):
[1 2 3 4 5]
[6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
Desired output (4,3):
[2 3 4]
[6 7 8]
[13 14 15]
[17 18 19]
where the starting index for each row is [2,1,3,2]
Thank you!
0 Commenti
Risposta accettata
Fangjun Jiang
il 20 Ott 2020
%%
Orig=transpose(reshape(1:20,5,[]))
StartPos=[2;1;3;2];
Wid=3;
row=repmat((1:size(Orig,1))',1,Wid);
col=StartPos+(0:Wid-1);
index=sub2ind(size(Orig),row,col);
out=Orig(index)
Più risposte (0)
Vedere anche
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!