How to find median value of a columns of a matrix ?

6 次查看(过去 30 天)
I have a zero-padded 8*8 matrix, and I want to find the median value of 3*3 block and then change the value of the middle element to this median value, then go to next block and do the same. how can I do this?
Here is my code:
A=[44 25 22 55 21 11;35 43 2 23 45 66;12 22 19 12 76 88; 21 23 43 12 65 75;...
10 86 56 55 23 97;23 75 88 5 55 71];
A_padded = padarray(A, [1 1]); % my 'zero-padding'
Thanks in advance for the help!

采纳的回答

Walter Roberson
Walter Roberson 2021-9-5
编辑:Walter Roberson 2021-9-5
A=[44 25 22 55 21 11;35 43 2 23 45 66;12 22 19 12 76 88; 21 23 43 12 65 75;...
10 86 56 55 23 97;23 75 88 5 55 71];
A_padded = padarray(A, [1 1]); % my 'zero-padding'
A_padded
A_padded = 8×8
0 0 0 0 0 0 0 0 0 44 25 22 55 21 11 0 0 35 43 2 23 45 66 0 0 12 22 19 12 76 88 0 0 21 23 43 12 65 75 0 0 10 86 56 55 23 97 0 0 23 75 88 5 55 71 0 0 0 0 0 0 0 0 0
simpliest_way = medfilt2(A_padded)
simpliest_way = 8×8
0 0 0 0 0 0 0 0 0 0 22 22 21 21 0 0 0 22 22 22 22 45 21 0 0 21 22 22 23 65 65 0 0 12 22 23 43 65 65 0 0 21 43 55 55 55 55 0 0 0 23 55 23 23 0 0 0 0 0 0 0 0 0 0
more_complicated = blockproc(A_padded, [1 1], @(B) median(B.data(:)), 'BorderSize', [1 1], 'TrimBorder', false)
more_complicated = 8×8
0 0 0 0 0 0 0 0 0 0 22 22 21 21 0 0 0 22 22 22 22 45 21 0 0 21 22 22 23 65 65 0 0 12 22 23 43 65 65 0 0 21 43 55 55 55 55 0 0 0 23 55 23 23 0 0 0 0 0 0 0 0 0 0
  5 个评论
Walter Roberson
Walter Roberson 2021-9-5
编辑:Walter Roberson 2021-9-5
What size of output are you expecting?
For example,
A B C D
E F G H
I J K L
First block would be [A B C] and you said you want to replace the middle with the median, which would give you [A M1 C] where M1 is a median. Then you slide over to [B C D] and you want to replace the middle with the median, which would give you [B M2 D] . Now you want to put those two together on output... and you would want the first output row to look like
[A M1 C B M2 D]
??? So if there were N columns of input, then you would want the output to have (N-2)*3 columns ??
Or do you want to output only the medians, so the output for the first row would be [M1 M2] ?
If you want to output only the medians, and it is for the 1 x 3 sliding, then the code is what I posted earlier,
more_complicated = blockproc(A_padded, [1 1], @(B) median(B.data(:)), 'BorderSize', [0 1], 'TrimBorder', false)
majid
majid 2021-9-5
I need the seond case.
fo matrix
A B C D
E F G H
I J K L
we first derive median of middle column (variable X1)and middle row (variable X2)
A B C
E F G
I J K
Then we go 1 element forward now we want to derive this matrix median
B C D
F G H
J K L
and so on!
finally we derive:
median of each rows : X1=[M1 M2 M3 ... Mn]
median of each column: X2=[N1 N2 N3 ... Nn]
do I explain clear?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by