Central part of a matrix
Afficher commentaires plus anciens
Hello,
If a have a Matrix M and I want to create a new one, call it Central_M, which should be the central part of M and of a defined size s, how can I do it?
M_central = ?
Thanks in advance for your reply.
2 commentaires
Chris McComb
le 24 Fév 2015
What exactly do you mean by 'central'? And can you define s?
Auryn_
le 25 Fév 2015
Réponses (2)
Jos (10584)
le 25 Fév 2015
Something like this?
M = spiral(10) % test matrix
S = 2 % s elements around the central position
% the resulting matrix will be 2*S+1 squared in size
dx = -S:S ;
RC0 = ceil(size(M)/2) % central position
OUT = M(RC0(1)+dx, RC0(2)+dx)
1 commentaire
Sean de Wolski
le 25 Fév 2015
+1 for spiral!
RAGHAVENDRA
le 25 Fév 2015
Modifié(e) : Guillaume
le 25 Fév 2015
M_central=M(floor(size(M,1)/2)-floor(s/2):floor(size(M,1)/2)+floor(s/2),floor(size(M,2)/2)-floor(s/2):floor(size(M,2)/2)+floor(s/2));
The above instruction will work, you can use 'round' instead of 'floor' to change the matrix indices accordingly.
3 commentaires
Guillaume
le 25 Fév 2015
Please use the {} Code button to format code in your answer
Auryn_
le 25 Fév 2015
RAGHAVENDRA
le 25 Fév 2015
Modifié(e) : RAGHAVENDRA
le 25 Fév 2015
It is working perfectly for me, this is the example that i have tried
M=randi(5,[10 10]);
s=2;
M_central=M(floor(size(M,1)/2)-floor(s/2):floor(size(M,1)/2)+floor(s/2),floor(size(M,2)/2)-floor(s/2):floor(size(M,2)/2)+floor(s/2))
What is the error message?
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!