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

New to MATLAB?

Thread Subject:
Another Matlab Array Index Question

Subject: Another Matlab Array Index Question

From: Jeff

Date: 14 Feb, 2010 04:40:23

Message: 1 of 3

I have the following code working, but I like Matlab's cool indexing syntax so much - and I wouldn't mind if my code ran faster and could show off to my professor and fellow students - that I want to make it work the Matlab array indexing way. You know, where you don't have to use loops to loop through a whole array. So how could I re-write this loop in a more Matlab style:

    for ii=1:throws
        if sign(darts(ii,2))==sign(darts(ii,3)) && abs(darts(ii,2))<=abs(darts(ii,3))

Basically, I need to do two comparisons of the cells in column 2 and 3. If I had to only compare the values of column 2 to column three, I could easily do this:


Ditto if I only had to compare the signs:


But I have to see if column 2 is the same sign as and smaller magnitude than column 3 (if column 2 is between the curve's y-value, in column 3, and the x-axis).

Math note not necessary to help with the code: this is a student simulation of Monte Carlo integrating: the curve's y-value is stored in column 3 and the random points are stored in column 2. I compare the points in column 2 to the value of the curve. The number of points below the curve, divided by the number of random points (darts) "thrown" times the total area of the box surrounding the domain and range, gives the area under the curve over the domain (got that!). But to compensate for when the curve is below the x-axis, I have to really find when the random point is between the curve and 0 - and that's what I'm trying to do.

P.S. I thought of the following while typing up this post. It seems less kludgey and uses no loops, but actually ran slower! What do you experts think?

    darts(:,6)=(darts(:,4) & darts(:,5)) .* sign(darts(:,3));


Subject: Another Matlab Array Index Question

From: Walter Roberson

Date: 14 Feb, 2010 04:51:53

Message: 2 of 3

Jeff wrote:
> So how could I re-write this
> loop in a more Matlab style:

> for ii=1:throws
> if sign(darts(ii,2))==sign(darts(ii,3)) &&
> abs(darts(ii,2))<=abs(darts(ii,3))
> darts(ii,4)=sign(darts(ii,3));
> end
> end
> hits=sum(darts(:,4))

d3s = sign(darts(:,3));
hits = sum( d3s( sign(darts(:,2)) == d3s) & ...
                  abs(darts(:,2)) <= abs(darts(:,3)) ) );

Subject: Another Matlab Array Index Question

From: Jeff

Date: 14 Feb, 2010 06:00:09

Message: 3 of 3

Walter Roberson <> wrote in message
> d3s = sign(darts(:,3));
> hits = sum( d3s( sign(darts(:,2)) == d3s) & ...
> abs(darts(:,2)) <= abs(darts(:,3)) ) );

Hi Walter. Who are you? You're answering everyone's questions tonight.

Thanks for that answer. It's really good. I got the idea you were trying to impart, but I had to switch it around some. I'm not sure what you meant by using the result of the logical & as an array index of d3s. I think you meant to multiply the final result by d3s to get the sign back. Really cool! Here's what I ended up with:

d3s = sign(darts(:,3));
hits = sum(d3s.*(sign(darts(:,2))==d3s & abs(darts(:,2)) <= abs(darts(:,3))));

This compares the signs of column 2 and 3, and logically ands it with a comparison of the magnitude of the values in col 2 & 3. If they agree, the result is a 1, which is again multiplied by the sign of column 3 (in the form of d3s) to get the correct sign back.

But this runs much slower than my looping solution. I guess sometimes elegant isn't always faster. Oh well. It was a good programming lesson. Thanks,

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