how to make a sub matrix from specific indices
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Walter Roberson
am 14 Apr. 2023
Bearbeitet: Walter Roberson
am 14 Apr. 2023
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 Kommentare
VBBV
am 14 Apr. 2023
Bearbeitet: VBBV
am 14 Apr. 2023
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{:}
Walter Roberson
am 14 Apr. 2023
Thanks for catching that, should be ind2sub()... I corrected my code.
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!