Thread Subject: changs arrays without using " if/end "

Subject: changs arrays without using " if/end "

From: Hosein

Date: 30 Jun, 2008 21:17:02

Message: 1 of 7

Assume that I have a matrix, say A, with 10^6 arrays. I want
to change any array which is less than zero to zero. Also, I
do not want to use 'if/end' and check each array.
Any idea?

Subject: changs arrays without using

From: John D'Errico

Date: 30 Jun, 2008 21:24:01

Message: 2 of 7

"Hosein " <Kalaeimh@yahoo.com> wrote in message
<g4bige$qqe$1@fred.mathworks.com>...
> Assume that I have a matrix, say A, with 10^6 arrays. I want
> to change any array which is less than zero to zero. Also, I
> do not want to use 'if/end' and check each array.
> Any idea?

help max

John

Subject: changs arrays without using " if/end "

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 30 Jun, 2008 21:26:13

Message: 3 of 7

In article <g4bige$qqe$1@fred.mathworks.com>,
Hosein <Kalaeimh@yahoo.com> wrote:
>Assume that I have a matrix, say A, with 10^6 arrays. I want
>to change any array which is less than zero to zero. Also, I
>do not want to use 'if/end' and check each array.
>Any idea?

I suspect that you mean that you have a matrix with 10^6 -entries-,
(or "array elements"), not with 10^6 sub-arrays. Otherwise it
would be necessary for you to define what it means for
an "array" to be less than 0.

If you are working with a numeric array, then

A(A < 0) = 0;

will do what you need.
--
  "There is nothing so bad but it can masquerade as moral."
                                              -- Walter Lippmann

Subject: changs arrays without using

From: Hosein

Date: 30 Jun, 2008 21:31:03

Message: 4 of 7

"John D'Errico" <woodchips@rochester.rr.com> wrote in
message <g4bith$16s$1@fred.mathworks.com>...
> "Hosein " <Kalaeimh@yahoo.com> wrote in message
> <g4bige$qqe$1@fred.mathworks.com>...
> > Assume that I have a matrix, say A, with 10^6 arrays. I want
> > to change any array which is less than zero to zero. Also, I
> > do not want to use 'if/end' and check each array.
> > Any idea?
>
> help max
>
> John

John;
What do you mean?
Assume that I am not looking for less than zero. I am
looking for a number, say '5'. I want to find any array
which equals to '5' and change it to zero.

Subject: changs arrays without using

From: Christopher Hummersone

Date: 30 Jun, 2008 21:32:01

Message: 5 of 7

"Hosein " <Kalaeimh@yahoo.com> wrote in message
<g4bige$qqe$1@fred.mathworks.com>...
> Assume that I have a matrix, say A, with 10^6 arrays. I want
> to change any array which is less than zero to zero. Also, I
> do not want to use 'if/end' and check each array.
> Any idea?

If by "array" you mean "element" (given that by my
definition an array is a matrix) then you can use logical
indexing. Code:

A = rand(5,99);
A(A<0.5) = 0;

will set all values in matrix 'A' that are less than 0.5 to 0.

Chris

Subject: changs arrays without using

From: Hosein

Date: 30 Jun, 2008 21:35:04

Message: 6 of 7

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g4bj1l$jfg$1@canopus.cc.umanitoba.ca>...
> In article <g4bige$qqe$1@fred.mathworks.com>,
> Hosein <Kalaeimh@yahoo.com> wrote:
> >Assume that I have a matrix, say A, with 10^6 arrays. I want
> >to change any array which is less than zero to zero. Also, I
> >do not want to use 'if/end' and check each array.
> >Any idea?
>
> I suspect that you mean that you have a matrix with 10^6
-entries-,
> (or "array elements"), not with 10^6 sub-arrays. Otherwise it
> would be necessary for you to define what it means for
> an "array" to be less than 0.
>
> If you are working with a numeric array, then
>
> A(A < 0) = 0;
>
> will do what you need.
> --
> "There is nothing so bad but it can masquerade as moral."
> -- Walter
Lippmann

You are right. Probably I should say "array elements"

Anyway. Your answer is exactly what I was looking for.
Thanks

Subject: changs arrays without using

From: John D'Errico

Date: 30 Jun, 2008 21:49:02

Message: 7 of 7

"Hosein " <Kalaeimh@yahoo.com> wrote in message
<g4bjan$52v$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in
> message <g4bith$16s$1@fred.mathworks.com>...
> > "Hosein " <Kalaeimh@yahoo.com> wrote in message
> > <g4bige$qqe$1@fred.mathworks.com>...
> > > Assume that I have a matrix, say A, with 10^6 arrays. I want
> > > to change any array which is less than zero to zero. Also, I
> > > do not want to use 'if/end' and check each array.
> > > Any idea?
> >
> > help max
> >
> > John
>
> John;
> What do you mean?
> Assume that I am not looking for less than zero. I am
> looking for a number, say '5'. I want to find any array
> which equals to '5' and change it to zero.

No, if you re-read your original statement,
you were looking for any element which was
less than zero, and changing that element
to zero. This does EXACTLY that.

  X = max(X,0);

If you change the problem, then I'd change
my answer. But does that really seem fair?

If you now want to reset any element which
was equal to 5, and turn those elements to
zero, then I'd have told you to do this:

  X(X==5) = 0;

Want to change it again? I hate chasing
moving targets, though. ;-)

John

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com