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:
if statements on scaler - possible to vectorize?

Subject: if statements on scaler - possible to vectorize?

From: Christian

Date: 7 Mar, 2014 15:05:13

Message: 1 of 11

Hi all,

I wonder if it's possible to vectorize if statements on a scalar. For the moment, I loop through each element of a matrix, check whether a condition is met for that statement and then execute an operation. Could this be vectorized? Does vectorization lead to speed improvements? Here's an example code, where all the matrices are of size IxJ.

The main idea is that I have a function w defined a grid yp, but now would like to assign those values in w to wn, which is defined over a slightly different grid, x. Both grids are strictly monotonically increasing in each column. xind gives the index of the nearest neighbor of yp in x, and xval=x(xind). If yp falls in between two grid points of x, I use weights stored in etadown and etaup to assign a portion of w to the upper point and another portion to the lower point.

Running the profiler, it is especially the if statements, particularly the one saying
elseif xind(i,j)==I && yp(i,j)>xval(i,j)
that takes up a lot of time. But maybe that's just a profiler problem because profiler needs 22 seconds for a code that only takes 5 seconds without profiling.

wn = zeros(I,J);
    for j=1:J
        for i = 1:I
            if xind(i,j)==1 && yp(i,j)<xval(i,j) % if y below x-grid
                wn(1,j)=wn(1,j)+w(i,j);
            elseif xind(i,j)==I && yp(i,j)>xval(i,j) % if y above x-grid
                wn(I,j)=wn(I,j)+w(i,j);
            else
                if xval(i,j)>yp(i,j) % nearest neighbor is above
                    wn(xind(i,j),j) = wn(xind(i,j),j) + w(i,j)*(1-etadown(i,j));
                    wn(xind(i,j)-1,j) = wn(xind(i,j)-1,j) + w(i,j)*etadown(i,j);
                else % nearest neighbor is below
                    wn(xind(i,j),j) = wn(xind(i,j),j) + w(i,j)*(1-etaup(i,j));
                    wn(xind(i,j)+1,j) = wn(xind(i,j)+1,j)+w(i,j)*etaup(i,j);
                end
            end
        end
    end

Thanks!

Subject: if statements on scaler - possible to vectorize?

From: dpb

Date: 8 Mar, 2014 14:00:12

Message: 2 of 11

On 3/7/2014 9:05 AM, Christian wrote:
> Hi all,
>
> I wonder if it's possible to vectorize if statements on a scalar. For
> the moment, I loop through each element of a matrix, check whether a
> condition is met for that statement and then execute an operation. Could
> this be vectorized? Does vectorization lead to speed improvements?
> Here's an example code, where all the matrices are of size IxJ.
> The main idea is that I have a function w defined a grid yp, but now
> would like to assign those values in w to wn, which is defined over a
> slightly different grid, x. Both grids are strictly monotonically
> increasing in each column. xind gives the index of the nearest neighbor
> of yp in x, and xval=x(xind). If yp falls in between two grid points of
> x, I use weights stored in etadown and etaup to assign a portion of w to
> the upper point and another portion to the lower point.
...

Isn't that

doc interp2 % ???

--

Subject: if statements on scaler - possible to vectorize?

From: Christian

Date: 8 Mar, 2014 17:17:08

Message: 3 of 11

dpb <none@non.net> wrote in message <lff7qj$dhm$1@speranza.aioe.org>...
> On 3/7/2014 9:05 AM, Christian wrote:
> > Hi all,
> >
> > I wonder if it's possible to vectorize if statements on a scalar. For
> > the moment, I loop through each element of a matrix, check whether a
> > condition is met for that statement and then execute an operation. Could
> > this be vectorized? Does vectorization lead to speed improvements?
> > Here's an example code, where all the matrices are of size IxJ.
> > The main idea is that I have a function w defined a grid yp, but now
> > would like to assign those values in w to wn, which is defined over a
> > slightly different grid, x. Both grids are strictly monotonically
> > increasing in each column. xind gives the index of the nearest neighbor
> > of yp in x, and xval=x(xind). If yp falls in between two grid points of
> > x, I use weights stored in etadown and etaup to assign a portion of w to
> > the upper point and another portion to the lower point.
> ...
>
> Isn't that
>
> doc interp2 % ???
>
> --

I think interp2 is doing something similar, but not exactly the same. I have to make sure that sum(w) gives me a vector of 1's because I interpret w as a pdf. So my weights are not the same as those used in interp2. I don't think that there's a built-in matlab function for updating pdfs. That's why I'm going the manual way.

Subject: if statements on scaler - possible to vectorize?

From: dpb

Date: 8 Mar, 2014 17:38:44

Message: 4 of 11

On 3/8/2014 11:17 AM, Christian wrote:
> dpb <none@non.net> wrote in message <lff7qj$dhm$1@speranza.aioe.org>...
>> On 3/7/2014 9:05 AM, Christian wrote:
...

>> > The main idea is that I have a function w defined a grid yp, but now
>> > would like to assign those values in w to wn, which is defined over a
>> > slightly different grid, x. ...
>> ...
>>
>> Isn't that
>>
>> doc interp2 % ???
...

> I think interp2 is doing something similar, but not exactly the same. I
> have to make sure that sum(w) gives me a vector of 1's because I
> interpret w as a pdf. So my weights are not the same as those used in
> interp2. I don't think that there's a built-in matlab function for
> updating pdfs. That's why I'm going the manual way.

doc filter2 % then

--

Subject: if statements on scaler - possible to vectorize?

From: Christian

Date: 8 Mar, 2014 19:42:07

Message: 5 of 11

Mhm, I'm not sure I understand how to apply the filter to my problem. Maybe it's possible to use filter instead of filter2 because my columns are independent and I don't filter across columns. How would you use the filter(2) function in my context?


dpb <none@non.net> wrote in message <lffkka$gmo$1@speranza.aioe.org>...
> On 3/8/2014 11:17 AM, Christian wrote:
> > dpb <none@non.net> wrote in message <lff7qj$dhm$1@speranza.aioe.org>...
> >> On 3/7/2014 9:05 AM, Christian wrote:
> ...
>
> >> > The main idea is that I have a function w defined a grid yp, but now
> >> > would like to assign those values in w to wn, which is defined over a
> >> > slightly different grid, x. ...
> >> ...
> >>
> >> Isn't that
> >>
> >> doc interp2 % ???
> ...
>
> > I think interp2 is doing something similar, but not exactly the same. I
> > have to make sure that sum(w) gives me a vector of 1's because I
> > interpret w as a pdf. So my weights are not the same as those used in
> > interp2. I don't think that there's a built-in matlab function for
> > updating pdfs. That's why I'm going the manual way.
>
> doc filter2 % then
>
> --

Subject: if statements on scaler - possible to vectorize?

From: dpb

Date: 8 Mar, 2014 20:55:01

Message: 6 of 11

On 3/8/2014 1:42 PM, Christian wrote:
> Mhm, I'm not sure I understand how to apply the filter to my problem.
> Maybe it's possible to use filter instead of filter2 because my columns
> are independent and I don't filter across columns. How would you use the
> filter(2) function in my context?
...
If you're only interpolating down the columns then yes, you don't need
the 2D version.

You say you're using two constants to adjust the values; fix the filter
coefficients to match.

Take some trial data and use filter on them to understand how it works
if you've not used it before.

--

Subject: if statements on scaler - possible to vectorize?

From: Christian

Date: 8 Mar, 2014 21:49:08

Message: 7 of 11

dpb <none@non.net> wrote in message <lfg04a$icf$1@speranza.aioe.org>...
> On 3/8/2014 1:42 PM, Christian wrote:
> > Mhm, I'm not sure I understand how to apply the filter to my problem.
> > Maybe it's possible to use filter instead of filter2 because my columns
> > are independent and I don't filter across columns. How would you use the
> > filter(2) function in my context?
> ...
> If you're only interpolating down the columns then yes, you don't need
> the 2D version.
>
> You say you're using two constants to adjust the values; fix the filter
> coefficients to match.
>
> Take some trial data and use filter on them to understand how it works
> if you've not used it before.
>
> --

My weights are not constant, but depend on the position of y relative to the grid vector x. That's why I'm not sure how to use the filter. I've stripped down my example and offer some numerical values. So here's the code that I want to speed up:

x=[2 4 5 7 8]'; % x_grid
y=[1.5 3 4.4 5.1 9]'; % y, to be mapped into x_grid
w=[.2 .4 .1 .2 .1]'; % w, pdf values associated with y
I = length(w);

[~,xidx] = histc(y,[-inf;(x(1:end-1)+x(2:end))/2;inf]); %nearest neighbor of y in x
    
y_in_x = x(xidx); % nearest on-grid point; y denotes off-grid points
weight1 = (y-y_in_x)./(x(max(xidx-1,1))-y_in_x); % weight if nearest neighbor above
weight2 = (y-y_in_x)./(x(min(xidx+1,I))-y_in_x); % weight if nearest neighbor below
    
wnew = zeros(I,1); % pdf values mapped into x_grid
for i=1:I
    if xidx(i)==1 && y(i)<y_in_x(i) % if y below x-grid
        wnew(1)=wnew(1)+w(i);
    elseif xidx(i)==I && y(i)>y_in_x(i) % if y above x-grid
        wnew(I)=wnew(I)+w(i);
    else
        if y_in_x(i)>y(i) % nearest neighbor is above
            wnew(xidx(i)) = wnew(xidx(i)) + w(i)*(1-weight1(i));
            wnew(xidx(i)-1) = wnew(xidx(i)-1) + w(i)*weight1(i);
        else % nearest neighbor is below
            wnew(xidx(i)) = wnew(xidx(i)) + w(i)*(1-weight2(i));
            wnew(xidx(i)+1) = wnew(xidx(i)+1)+w(i)*weight2(i);
        end
    end
end

The resulting vector wnew is [0.4 0.26 0.23 0.01 0.1].

In the for-loop, the first two cases deal with extreme cases, i.e. values of y that are either below the smallest x value or above the largest x value; the third case looks at intermediate cases and I have to distinguish between the case, where the nearest neighbor of y in x is above or below y. If there's any possibility to speed this up, maybe using filter, maybe through vectorizing the code, that would be great.

Subject: if statements on scaler - possible to vectorize?

From: dpb

Date: 8 Mar, 2014 23:47:09

Message: 8 of 11

On 3/8/2014 3:49 PM, Christian wrote:
...

> My weights are not constant, but depend on the position of y relative to
> the grid vector x. That's why I'm not sure how to use the filter. I've
> stripped down my example and offer some numerical values. So here's the
> code that I want to speed up:
...

OK, the coefficients would be dynamic, then.

I don't have time just now to really work on it so these have just been
otoh thoughts. Maybe tomorrow I can look at it a little more...

One last thought though -- why wouldn't it be just as good to use
interp1() and then normalize the result for the sum instead of worrying
about that until the end? The shape should still reflect the other from
which it's interpolated. If it's very nonlinear then the spline might
be better than linear.

--

Subject: if statements on scaler - possible to vectorize?

From: dpb

Date: 9 Mar, 2014 00:20:07

Message: 9 of 11

On 3/8/2014 3:49 PM, Christian wrote:
...

> ... offer some numerical values. ...
>
> x=[2 4 5 7 8]'; % x_grid
> y=[1.5 3 4.4 5.1 9]'; % y, to be mapped into x_grid
> w=[.2 .4 .1 .2 .1]'; % w, pdf values associated with y
...

Have you plotted these???? Doesn't look like anything to me.

--

Subject: if statements on scaler - possible to vectorize?

From: Christian

Date: 9 Mar, 2014 00:31:14

Message: 10 of 11

dpb <none@non.net> wrote in message <lfga77$cbm$1@speranza.aioe.org>...
> On 3/8/2014 3:49 PM, Christian wrote:
> ...
>
> > My weights are not constant, but depend on the position of y relative to
> > the grid vector x. That's why I'm not sure how to use the filter. I've
> > stripped down my example and offer some numerical values. So here's the
> > code that I want to speed up:
> ...
>
> OK, the coefficients would be dynamic, then.
>
> I don't have time just now to really work on it so these have just been
> otoh thoughts. Maybe tomorrow I can look at it a little more...
>
> One last thought though -- why wouldn't it be just as good to use
> interp1() and then normalize the result for the sum instead of worrying
> about that until the end? The shape should still reflect the other from
> which it's interpolated. If it's very nonlinear then the spline might
> be better than linear.
>
> --

Thanks for looking into it. I think interpolation is too different from what my code is doing. Imagine w=ones(1,5)/5 and y=5*ones(1,5). Then, interp1(y,w,x) would break down. My code would generate wnew=[0 0 1 0 0] because all the mass of the pdf would be at x(3).

Here's an example to motivate my code:
There are 5 groups of people with w denoting the number of people in each group.
Each group chooses a number on a scale from 0 to 10. This number is stored in y.

Now, I'm interested in knowing how many people have chosen the numbers stored in x, or: have chosen numbers close to those in x. People choosing numbers that are not in x have to be assigned by some algorithm to those in x. My specific algorithm says that e.g. those people choosing 4.4 are assigned to the two neighbors in x: 4 and 5. Since 4.4 is 40% between 4 and 5, I assign 60% of the people to 4, and 40% of the people to 5.

To come back to my extreme example above: If all the people choose y=5, then 100% of all the people will be assigned to x=5, which is reflected by wnew.

This is a standard algorithm of assigning off-grid values to on-grid values, but I'm happy to learn other ways how to assign the people choosing 4.4 to the points in x.

Subject: if statements on scaler - possible to vectorize?

From: Christian

Date: 9 Mar, 2014 01:11:12

Message: 11 of 11

dpb <none@non.net> wrote in message <lfgc51$gn2$1@speranza.aioe.org>...
> On 3/8/2014 3:49 PM, Christian wrote:
> ...
>
> > ... offer some numerical values. ...
> >
> > x=[2 4 5 7 8]'; % x_grid
> > y=[1.5 3 4.4 5.1 9]'; % y, to be mapped into x_grid
> > w=[.2 .4 .1 .2 .1]'; % w, pdf values associated with y
> ...
>
> Have you plotted these???? Doesn't look like anything to me.
>
> --

Sorry, just saw this message. Yes, I've plotted those vectors. I don't know what you expect them to look like. I agree the pdf is a bit funky and doesn't have a nice bell shape, but those are just random numbers. w must just satisfy the standard conditions for a pdf. x is strictly monotonically increasing, while y doesn't have to be monotonically increasing.

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