フィルターのクリア

Central part of a matrix

1 回表示 (過去 30 日間)
Auryn_
Auryn_ 2015 年 2 月 24 日
コメント済み: Sean de Wolski 2015 年 2 月 25 日
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 件のコメント
Chris McComb
Chris McComb 2015 年 2 月 24 日
What exactly do you mean by 'central'? And can you define s?
Auryn_
Auryn_ 2015 年 2 月 25 日
Central means that I want to take only the central part of the matrix of a defined size s. s = imcrop(I,[max(1,i-10) max(1,j-10) 23 23]);

サインインしてコメントする。

回答 (2 件)

Jos (10584)
Jos (10584) 2015 年 2 月 25 日
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 件のコメント
Sean de Wolski
Sean de Wolski 2015 年 2 月 25 日
+1 for spiral!

サインインしてコメントする。


RAGHAVENDRA
RAGHAVENDRA 2015 年 2 月 25 日
編集済み: Guillaume 2015 年 2 月 25 日
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 件のコメント
Auryn_
Auryn_ 2015 年 2 月 25 日
I receive an error message :(
RAGHAVENDRA
RAGHAVENDRA 2015 年 2 月 25 日
編集済み: RAGHAVENDRA 2015 年 2 月 25 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by