Program to find the median of the matrix (need to use for loop)

2 ビュー (過去 30 日間)
Manpreet Kapoor
Manpreet Kapoor 2011 年 4 月 17 日
Hi,
I need to make a progrqam for the following logic:
1. Consider a matrix of size 120x120. Now consider a section of 3x3 and find the median of the 3x3 section, replacing the values of the matrix to make a new matrix (of a smaller size). The new 3x3 section will be taking the next element as the central element.
2. Please see that there is a command in MATLAB, called medfilt, medfilt2, which does the same, but I need to develop a program which does this manually i.e. it first sorts our the elements in ascending or descending order and then selects the middle value as the median thereby forming a new matrix with the median values.
The program that I am building is more complex than the section which I need help on. Any help in deleveloping this program will be highly appreciated.
Thanks and Regards
Manpreet Kaur
  2 件のコメント
bym
bym 2011 年 4 月 17 日
what have you tried so far?
Paulo Silva
Paulo Silva 2011 年 4 月 17 日
interesting homework!

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

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2011 年 4 月 17 日
variant % size(Min) = [m1 n1], size(Msection) = [m2 n2]
mn1 = size(Min)+2;
Min1 = zeros(mn1);
Min1(2:end-1,2:end-1) = Min;
Mout = zeros(mn1-2);
IJ = arrayfun(@(x)subsref(buffer(1:x,3,2),struct('type','()','subs',{{':',3:x}})),mn1,'UniformOutput', false);
[I,J] = IJ{:};
for ii = 1:length(I)
for jj = 1:length(J)
Mout(ii,jj) = median(median(Min1(I(:,ii),J(:,jj))));
end
end
as in the previous variant without a loop (as in medfilt2)
mn1 = size(Min)+2;
Min1 = zeros(mn1);
Min1(2:end-1,2:end-1) = Min;
IJ = arrayfun(@(x)subsref(buffer(1:x,3,2),struct('type','()','subs',{{':',3:x}})),mn1,'Un', false);
[I,J] = IJ{:};
[j3,i3] = meshgrid((1:mn1(2)-2),(1:mn1(1)-2));
Mout = arrayfun(@(x,y)median( reshape(Min1(I(:,x),J(:,y)),[],1)),i3,j3);
  2 件のコメント
Manpreet Kapoor
Manpreet Kapoor 2011 年 4 月 18 日
i dint get wat is m1, n1 and m2,n2?
plz describe all the notations.....
Andrei Bobrov
Andrei Bobrov 2011 年 4 月 18 日
analogy to your task: m1 = 120, n1 = 120, m2 = 3, n2 = 3

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


Manpreet Kapoor
Manpreet Kapoor 2011 年 4 月 21 日
mr. Andrei, I went through ur solution, but it does not sort out the elements in consideration. U have created a matrix of 0s which does not serve the purpose. Please help..... Regards Manpreet Kaur
  3 件のコメント
Manpreet Kapoor
Manpreet Kapoor 2011 年 4 月 27 日
Mr. Matt,
I have crossed the age where u get homeworks. I am now a lecturer and give homeworks to budding engineers. I joined this site not to get some homework done, but to learn MATLAB, hence posted the question.
Regards
Manpreet Kaur
Sean de Wolski
Sean de Wolski 2011 年 4 月 27 日
I wouldn't want to take a class taught by someone who: A) doesn't know how to follow instructions (this post, and your identical duplicate post) and B) Can't read documentation. $0.02

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

カテゴリ

Help Center および File ExchangeSmoothing and Denoising についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by