How to extract matrix with different starting index for each row?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Katherine Liu
am 20 Okt. 2020
Kommentiert: Katherine Liu
am 20 Okt. 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 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 20 Okt. 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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!