Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Successive Matrix Midpoint Operations

Subject: Successive Matrix Midpoint Operations

From: K

Date: 20 Mar, 2013 17:27:17

Message: 1 of 6

I'm having a tough time understanding how to do this properly.

Imagine I have a matrix = NaN(9,1) with values at matrix(1,1) = 1 and matrix(end, 1) = 9.
So: [1;NaN;NaN;NaN;NaN;NaN;NaN;NaN;9]. I want to perform a mean of the numbers and place it in the center of the matrix so: [1;NaN;NaN;NaN;5.5;NaN;NaN;NaN;9], then I want to take a mean from the first to the center value and the mean from the center to the last value, so: [1;NaN;3.25;NaN;5.5;NaN;7.25;NaN;9] and so on until I have this: [1;2.125;3.25;4.375;5.5;6.35;7.25;8.125;9]

I'm using mean as just an example in this case; I wish to perform other operations as I go, so the result won't be as uniform as described above, but the pattern is the same.

Thanks for any insight,
KP

Subject: Successive Matrix Midpoint Operations

From: someone

Date: 20 Mar, 2013 17:58:14

Message: 2 of 6

"K" wrote in message <kicrhl$2r2$1@newscl01ah.mathworks.com>...
> I'm having a tough time understanding how to do this properly.
>
> Imagine I have a matrix = NaN(9,1) with values at matrix(1,1) = 1 and matrix(end, 1) = 9.
> So: [1;NaN;NaN;NaN;NaN;NaN;NaN;NaN;9]. I want to perform a mean of the numbers and place it in the center of the matrix so: [1;NaN;NaN;NaN;5.5;NaN;NaN;NaN;9], then I want to take a mean from the first to the center value and the mean from the center to the last value, so: [1;NaN;3.25;NaN;5.5;NaN;7.25;NaN;9] and so on until I have this: [1;2.125;3.25;4.375;5.5;6.35;7.25;8.125;9]


A couple of questions/clarifications - First, why wouldn't the first result be:

[1;NaN;NaN;NaN;5;NaN;NaN;NaN;9]

Second, is your vector (matrix) ALWAYS of odd length? Even so, what if its something like:

[1;NaN;NaN;NaN;NaN;NaN;7]

what do you want on the second iteration of:

[1;NaN;NaN;4;NaN;NaN;7]

Do you want:

[1;2.5;2.5;4;5.5;5.5;7]

or

[1;2;3;4;5;6;7]



>
> I'm using mean as just an example in this case; I wish to perform other operations as I go, so the result won't be as uniform as described above, but the pattern is the same.
>
> Thanks for any insight,
> KP

Subject: Successive Matrix Midpoint Operations

From: K

Date: 20 Mar, 2013 19:51:32

Message: 3 of 6

> A couple of questions/clarifications - First, why wouldn't the first result be:
> [1;NaN;NaN;NaN;5;NaN;NaN;NaN;9]
> Second, is your vector (matrix) ALWAYS of odd length? Even so, what if its something like:
> [1;NaN;NaN;NaN;NaN;NaN;7]
> what do you want on the second iteration of:
> [1;NaN;NaN;4;NaN;NaN;7]
> Do you want:
> [1;2.5;2.5;4;5.5;5.5;7]>
> or
> [1;2;3;4;5;6;7]

Hi!

I have no idea why I put 5.5 that first time around, it would indeed be 5.

The matrix should always divide evenly by twos between midpoints, so I suppose it would always be odd. The matrices will follow a 2x-1 pattern, so: 3, 5, 9, 17 length matrix.

I've figured out how to make it work in a clunky way with this 17 length matrix:

%Establish the original matrix
POINTS = NaN (17,1);
%Populate first and last values:
POINTS(1) = 1;
POINTS(end) = 17;

%4 iterations for a matrix of length 17
for h = 1:4
        idxx = find(~isnan(POINTS(:))); %find the indexes of values
        cnt = 2; %start a counter
        for j = idxx(1:end-1)' %start from index of 1 to second to last index value
            
            x = POINTS(j); %first point
            y = POINTS(idxx(cnt)); %second point
            m = mean([x y]); %mean
                    
            POINTS(mean([j idxx(cnt)])) = m; %location to put new value
            cnt = cnt+1; %increase count
        end
end


Any input is appreciated!

Subject: Successive Matrix Midpoint Operations

From: Bruno Luong

Date: 20 Mar, 2013 20:52:05

Message: 4 of 6

% Save those in midfill.m

function a = midfill(a, fun)
a = md(a, 1, length(a), fun);
end

function a = md(a, istart, istop, fun)
if istop > istart+1
    imid = floor(0.5*(istart+istop));
    a(imid) = fun(a([istart istop]));
    a = md(a, istart, imid, fun);
    a = md(a, imid, istop, fun);
end
end

>> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],@mean)

ans =

     1 2 3 4 5 6 7 8 9

>> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],@sum)

ans =

     1 12 11 21 10 29 19 28 9

>> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],@prod)

ans =

     1 9 9 81 9 729 81 729 9

% Bruno

Subject: Successive Matrix Midpoint Operations

From: K

Date: 21 Mar, 2013 18:30:15

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kid7hl$e9l$1@newscl01ah.mathworks.com>...
> % Save those in midfill.m
>
> function a = midfill(a, fun)
> a = md(a, 1, length(a), fun);
> end
>
> function a = md(a, istart, istop, fun)
> if istop > istart+1
> imid = floor(0.5*(istart+istop));
> a(imid) = fun(a([istart istop]));
> a = md(a, istart, imid, fun);
> a = md(a, imid, istop, fun);
> end
> end
>
> >> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],@mean)
>
> ans =
>
> 1 2 3 4 5 6 7 8 9
>
> >> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],@sum)
>
> ans =
>
> 1 12 11 21 10 29 19 28 9
>
> >> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],@prod)
>
> ans =
>
> 1 9 9 81 9 729 81 729 9
>
> % Bruno

Bruno, this is excellent and so compact!

I'm going to try to see if I can work in my own operation into the code. Although the 'fun' tag is incredibly useful, the operation I want to perform is not a standard one.

Thank you so much for your time and brain cycles,
KP

Subject: Successive Matrix Midpoint Operations

From: Bruno Luong

Date: 21 Mar, 2013 21:58:06

Message: 6 of 6

"K" wrote in message <kifjjn$q7s$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> I'm going to try to see if I can work in my own operation into the code. Although the 'fun' tag is incredibly useful, the operation I want to perform is not a standard one.
>

But what make you think that fun must be a standard one? Fun is a generic function that takes any 1x2 array and returns a result.

>> fun = @(a) 1/a(1) + 3*a(2)

fun =

    @(a)1/a(1)+3*a(2)

>> midfill([1 NaN NaN NaN NaN NaN NaN NaN 9],fun)

ans =

    1.0000 256.0000 85.0000 84.0118 28.0000 81.1429 27.0357 27.0370 9.0000

% Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us