how to make a sub matrix from specific indices
3 次查看(过去 30 天)
显示 更早的评论
I have a 1400x1400 matrix (A), from this I want to make a 10x10 submatrix but I want it at the followign indices:
312
323
673
876
1031
1326
1344
1354
1359
1384
is this possible?
Thank you!
0 个评论
回答(1 个)
Walter Roberson
2023-4-14
编辑:Walter Roberson
2023-4-14
sizeA = [1400 1400];
indices = [312
323
673
876
1031
1326
1344
1354
1359
1384];
[r, c] = ind2sub(sizeA, indices);
nummat = size(r,1);
mats = cell(nummat,1);
for K = 1 : nummat
mats{K} = A(r(K):r(K)+9, c(K):c(K)+9);
end
Yes, it would be possible to vectorize this a bit, but the code would be notably more difficult to understand.
2 个评论
VBBV
2023-4-14
编辑:VBBV
2023-4-14
Both ind2sub & sub2ind are able to produce the 10 x10 matrix format, but which one is correct here is confusing for me,
sizeA = [1400 1400];
A = randn(1400);
indices = [312
323
673
876
1031
1326
1344
1354
1359
1384];
[r c] = ind2sub(sizeA, indices);
nummat = size(r,1);
mats = cell(nummat,1);
for K = 1 : nummat
mats{K} = A(r(K):r(K)+9, c(K):c(K)+9);
end
mats{:}
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!