numpy/matlab compatibility
Matt Funk
mafunk at nmsu.edu
Tue Jan 25 16:58:00 EST 2011
Hi,
thank you Andrea. That is exactly what i was looking for. Great.
Andrea explained what the Matlab code does below. Sorry about the
confusion.
I was under the impression that numpy was leaning very heavily on Matlab
for its syntax and thus i assumed that
Matlab was mostly known for those using numpy.
Andrea:
you are right about the value 100. It should have been 0.5.
The original code has a different vector which is tested against 100. I
tried to simply reproduce the functionality with a random vector.
Thus the confusion.
Again, thanks for the input.
matt
On 1/25/2011 2:36 PM, Andrea Ambu wrote:
>
>
> On Tue, Jan 25, 2011 at 9:13 PM, Matt Funk <mafunk at nmsu.edu
> <mailto:mafunk at nmsu.edu>> wrote:
>
> Hi,
>
> i am fairly new to python. I was wondering of the following is do-able
> in python:
>
> 1) a = rand(10,1)
> 2) Y = a
> 3) mask = Y > 100;
> 4) Y(mask) = 100;
> 5) a = a+Y
>
>
> No. Not like that.
>
> You do literally:
> a = rand(10, 1)
> Y = a
> mask = Y>100
> Y = where(mask, 100, Y)
> a = a+Y
>
>
> More Pythonically:
> a = rand(10, 1)
> a = where(a > 100, a + 100, a + a)
>
>
> For those who don't speak Matlab:
>
> 1) a = rand(10,1) ; generates a 10x1 matrix for random number 0 < n < 1
> 2) Y = a
> 3) mask = Y > 100; similar to: mask = [i>100 for i in Y]
> 4) Y(mask) = 100; sets to 100 elements of Y with index i for which
> mask[i] = True
> 5) a = a+Y ; sums the two matrices element by element (like you do in
> linear algebra)
>
>
> Anyway... rand generates number from 0 up to 1 (both in python and
> matlab)... when are they > 100?
>
>
>
> Basically i am getting stuck on line 4). I was wondering if it is
> possible or not with python?
> (The above is working matlab code)
>
> thanks
> matt
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110125/6e7b9b5a/attachment-0001.html>
More information about the Python-list
mailing list