Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:06:05+00:00 2026-05-14T03:06:05+00:00

Let’s assume we have two arrays of the same size – A and B

  • 0

Let’s assume we have two arrays of the same size – A and B.

Now, we need a filter that, for a given mask size, selects elements from A, but removes the central element of the mask, and inserts there corresponding element from B.

So the 3×3 “pseudo mask” will look similar to this:

A A A
A B A
A A A

Doing something like this for averaging filter is quite simple. We can compute the mean value for elements from A without the central element, and then combine it with a proper proportion with elements from B:

h = ones(3,3);
h(2,2) =0; 
h = h/sum(h(:));
A_ave = filter2(h, A);
C = (8/9) * A_ave + (1/9) * B;

But how to do something similar for median filter (medfilt2 or even better for ordfilt2)

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:06:05+00:00Added an answer on May 14, 2026 at 3:06 am

    The way to solve this is to find a way to combine the information from A and B so that the filtering itself becomes easy.

    The first thing I thought of was to catenate A and B along the third dimension and to pass with a filter mask that would take 8 elements from the ‘A-slice’ and the center element from the ‘B-slice’. This is, unfortunately, not supported by Matlab.

    While nlfilter only works on 2D images, it does allow you to specify any function for filtering. Thus, you could create a function that somehow is able to look up the right values of A and B. Thus I came to my first solution.

    You create a new array, C, that contains the element index at each element, i.e. the first element is 1, the second element is 2, etc. Then, you run nlfilter, which takes a 3×3 sliding window and passes the values of C inside the window to the filtering function, ffn. ffn is an anonymous function, that calls crazyFilter, and that has been initialized so that A and B get passed at each call. CrazyFunction takes the values from the sliding window of C, which are nothing but indices into A and B, and collects the values from A and B from them.

    The second solution is exactly the same, except that instead of moving a sliding window, you create a new array that, in every column, has the contents of the sliding window at every possible location. With an overlapping window, the column array gets larger than the original array. Again, you then just need to use the values of the column array, C, which are indices into A and B, to look up the values of A and B at the relevant locations.

    EDIT
    If you have enough memory, im2col and col2im can speed up the process a lot

    %# define A,B
    A = randn(100);
    B = rand(100);
    
    %# pad A, B - you may want to think about how you want to pad
    Ap = padarray(A,[1,1]);
    Bp = padarray(B,[1,1]);
    
    #% EITHER -- the more more flexible way
    %# create a pseudo image that has indices instead of values
    C = zeros(size(Ap));
    C(:) = 1:numel(Ap);
    %# convert to 'column image', where each column represents a block
    C = im2col(C,[3,3]);
    %# read values from A
    data = Ap(C);
    %# replace centers with values from B
    data(5,:) = Bp(C(5,:));
    
    %# OR -- the more efficient way
    %# reshape A directly into windows and fill in B
    data = im2col(Ap,[3,3]);
    data(5,:) = B(:);
    
    % median and reshape
    out = reshape(median(data,1),size(A));
    

    Old version (uses less memory, may need padding)

    %# define A,B
    A = randn(100);
    B = rand(100);
    
    %# define the filter function
    ffun = @(x)crazyFilter(x,A,B);
    
    %# create a pseudo image that has indices instead of values
    C = zeros(size(A));
    C(:) = 1:numel(A);
    
    %# filter
    filteredImage = nlfilter(C,[3,3],ffun);
    
    
    
    
    %# filter function
    function out = crazyFilter(input,A,B)
    %#CRAZYFILTER takes the median of a 3x3 mask defined by input, taking 8 elements from A and 1 from B
    
    %# read data from A
    data = A(input(:));
    %# replace center element with value from B
    data(5) = B(input(5));
    
    %# return the median
    out = median(data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let assume we have two activities. A - main activity, that is home launcher
Let's say I have two text files that I need to extract data out
Let me try to explain what I need. I have a server that is
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's assume that we are building a high traffic site that will be used
Let's say I have multiple requirements for a password. The first is that the
Let's assume that a user votes for some movies in a scale of 1
Let's say that I have a date in R and it's formatted as follows.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.