How to extract matrix with different starting index for each row?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Katherine Liu
le 20 Oct 2020
Commenté : Katherine Liu
le 20 Oct 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 commentaires
Réponse acceptée
Fangjun Jiang
le 20 Oct 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)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!