How to extract matrix with different starting index for each row?

6 次查看(过去 30 天)
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!

采纳的回答

Fangjun Jiang
Fangjun Jiang 2020-10-20
%%
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by