Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Multiplying Double and a Logical
Replies: 5   Last Post: Jun 21, 2012 10:25 AM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: Multiplying Double and a Logical
Posted: Jun 18, 2012 7:52 PM

On 6/18/2012 6:09 PM, Michael wrote:
> Hello all. This is my first post and this may be a silly question, but I
> googled the shit out of this to no avail...
>
> I have an array with some NaNs (which is a double), the array is also,
> therefore, a double). I'd like to (point-wise-.*) multiply this with
> another array (equal dimension) that is a logical.

Well, a Matlab logical is also a double w/ the logical internal flag set
so if you really want a point-by-point multiply of conformant arrays,
just do it...

>> d=1:3;
>> l=logical([0 1 0]);
>> whos d l

Name Size Bytes Class

d 1x3 24 double array
l 1x3 24 double array (logical)

>> p=d.*l
p =
0 2 0
>> whos p
Name Size Bytes Class

p 1x3 24 double array

>>

...

> Essentially I'm trying to plot something like [ 1 2 3 NaN] with [ 1 3 5
> 7] (as crude examples). When plotting I'd only get three points (exactly
> as I want). But I'm making a 'cut' in the data by multiplying a logical
> (the cut-composed of 1's and 0's) and a double (the actual data
> containing some NaNs).
>

I can't follow precisely the question...use of NaN to not display
selected points in plot() and friends is a well-respected technique but
that seems to be ok if I get your drift.

If you want some other selection criterion as well, use of logical
addressing is one way but you don't multiply, you use the logical
subscript to select the points desired.

Try

x=[1 2 3 NaN];
y=[1 3 5 7];
l=logical(1 1 0 1);
plot(x(l),y(l)

Is that the desired result?

Read section on 'Logical Subscripting" in the Getting Started online doc's.

--

Date Subject Author
6/18/12 Michael
6/18/12 dpb
6/18/12 Michael
6/18/12 Nasser Abbasi
6/18/12 dpb
6/21/12 Michael