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!