From: jacob cohen on
I would like to write a fast program which sets all negative values in an array to zero. I have tried this using two for loops an an if statement but it is way too slow for my large array sizes. My arrays are 5000 x 600, so I need something better than what I have.

I have thought about using the find function but I haven't tried that yet. Are there any built in functions which would do this for me??

Thanks,
Jake
From: us on
"jacob cohen" <jcohen7(a)gatech.edu> wrote in message <holn46$1ru$1(a)fred.mathworks.com>...
> I would like to write a fast program which sets all negative values in an array to zero. I have tried this using two for loops an an if statement but it is way too slow for my large array sizes. My arrays are 5000 x 600, so I need something better than what I have.
>
> I have thought about using the find function but I haven't tried that yet. Are there any built in functions which would do this for me??
>
> Thanks,
> Jake

one of the many solutions

v=[-1,2,-3,4,0,-5,6];
v(v<0)=0
% v = 0 2 0 4 0 0 6

us
From: Walter Roberson on
jacob cohen wrote:
> I would like to write a fast program which sets all negative values in
> an array to zero.

A = max(0,A);