how to make a sub matrix from specific indices
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Walter Roberson
il 14 Apr 2023
Modificato: Walter Roberson
il 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 Commenti
VBBV
il 14 Apr 2023
Modificato: VBBV
il 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
il 14 Apr 2023
Thanks for catching that, should be ind2sub()... I corrected my code.
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!