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

Thread Subject:
matlab refuse to set matrix values to 0

Subject: matlab refuse to set matrix values to 0

From: besbesmany besbesmany

Date: 9 Jan, 2012 22:22:08

Message: 1 of 14

i've matrix with values i want to set values <0 to 0 and values >1 to 1

but it's not set as i need

A(A(1:m,:) < 0)=0;

also tried this
indices=find(A(1:m,:)<0);
A(indices)=0;

sample of matrix after setting code

2.22E-16 0.11127058 0.119648265 0.015795849 0.015795849 -0.054556464
1 0.016568727 0.002217947 0.01670619 0.009019064 2.22E-17
1 0.012667205 0.059356222 0.007550615 0.004826279 -0.01701843
0.937895091 5.39E-02 0.014343948 0.123148877 4.13E-02 -0.070609789

Subject: matlab refuse to set matrix values to 0

From: James Tursa

Date: 9 Jan, 2012 23:06:08

Message: 2 of 14

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jefpag$841$1@newscl01ah.mathworks.com>...
> i've matrix with values i want to set values <0 to 0 and values >1 to 1
>
> but it's not set as i need
>
> A(A(1:m,:) < 0)=0;
>
> also tried this
> indices=find(A(1:m,:)<0);
> A(indices)=0;
>
> sample of matrix after setting code
>
> 2.22E-16 0.11127058 0.119648265 0.015795849 0.015795849 -0.054556464
> 1 0.016568727 0.002217947 0.01670619 0.009019064 2.22E-17
> 1 0.012667205 0.059356222 0.007550615 0.004826279 -0.01701843
> 0.937895091 5.39E-02 0.014343948 0.123148877 4.13E-02 -0.070609789

From your description, it seems like this should work:

A(A<0) = 0;
A(A>1) = 1;

Is there some reason why you are using extra indexing in your code?

James Tursa

Subject: matlab refuse to set matrix values to 0

From: ScottB

Date: 9 Jan, 2012 23:07:08

Message: 3 of 14

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jefpag$841$1@newscl01ah.mathworks.com>...
> i've matrix with values i want to set values <0 to 0 and values >1 to 1
>
> but it's not set as i need
>
> A(A(1:m,:) < 0)=0;
>
> also tried this
> indices=find(A(1:m,:)<0);
> A(indices)=0;
>
> sample of matrix after setting code
>
> 2.22E-16 0.11127058 0.119648265 0.015795849 0.015795849 -0.054556464
> 1 0.016568727 0.002217947 0.01670619 0.009019064 2.22E-17
> 1 0.012667205 0.059356222 0.007550615 0.004826279 -0.01701843
> 0.937895091 5.39E-02 0.014343948 0.123148877 4.13E-02 -0.070609789

You're getting floating point representations of zero. You can assume values around eps to be zero (set them). Take a look at the command "eps".

ScottB

Subject: matlab refuse to set matrix values to 0

From: besbesmany besbesmany

Date: 10 Jan, 2012 09:34:09

Message: 4 of 14

can you tell me what to do exactly
do you mean A(A(1:m,:) < 0)=eps
i use 1:m because i update row by row in this matrix

i 've 50 iterations and this error appears in 20th or 30th iterations

i mean setting -ve values to 0 is ok in some iterations but the problem appears later iterations so it gives me wrong result

"ScottB" wrote in message <jefrus$g1s$1@newscl01ah.mathworks.com>...
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jefpag$841$1@newscl01ah.mathworks.com>...
> > i've matrix with values i want to set values <0 to 0 and values >1 to 1
> >
> > but it's not set as i need
> >
> > A(A(1:m,:) < 0)=0;
> >
> > also tried this
> > indices=find(A(1:m,:)<0);
> > A(indices)=0;
> >
> > sample of matrix after setting code
> >
> > 2.22E-16 0.11127058 0.119648265 0.015795849 0.015795849 -0.054556464
> > 1 0.016568727 0.002217947 0.01670619 0.009019064 2.22E-17
> > 1 0.012667205 0.059356222 0.007550615 0.004826279 -0.01701843
> > 0.937895091 5.39E-02 0.014343948 0.123148877 4.13E-02 -0.070609789
>
> You're getting floating point representations of zero. You can assume values around eps to be zero (set them). Take a look at the command "eps".
>
> ScottB

Subject: matlab refuse to set matrix values to 0

From: Roger Stafford

Date: 10 Jan, 2012 20:08:08

Message: 5 of 14

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jeh0mh$1l4$1@newscl01ah.mathworks.com>...
> can you tell me what to do exactly
> do you mean A(A(1:m,:) < 0)=eps
> i use 1:m because i update row by row in this matrix
>
> i 've 50 iterations and this error appears in 20th or 30th iterations
>
> i mean setting -ve values to 0 is ok in some iterations but the problem appears later iterations so it gives me wrong result
>
> "ScottB" wrote in message <jefrus$g1s$1@newscl01ah.mathworks.com>...
> > You're getting floating point representations of zero. You can assume values around eps to be zero (set them). Take a look at the command "eps".
> >
> > ScottB
- - - - - - - - -
  There is something very wrong with your computations. You should be getting exact zeros wherever you have an assignment indicated by "= 0" and exact ones with "= 1". There is no question of a floating point representation of these integer values being anything other than exactly correct.

  I think you had better show us more of the details of your procedure. You say "i update row by row in this matrix", but writing

 A(A(1:m,:) < 0)=0;

is not a row by row process. It deals with all rows from 1 to m together.

  Moreover, if the value of m is allowed to be anything other than size(A,1), you will surely get very erroneous results. This is also true with your use of the 'find' function. At best you would have the wrong elements being indexed.

  Please show us precisely what you are doing. If it is placed within a loop show us the whole loop.

Roger Stafford

Subject: matlab refuse to set matrix values to 0

From: besbesmany besbesmany

Date: 11 Jan, 2012 10:49:08

Message: 6 of 14

i found the problem as follows
it takes the index of -ve value in wrong way
i mean if i want to make A(2,6)=0 it makes A(12)=0
i mean A(A(1:2,:) < 0)=0; it understand it as A(12,1)=0 and not
A(2,6)=0

so how can i make this code in both ways
A(A(1:m,:) < 0)=0;
and
indices=find(A(1:m,:)<0);
A(indices)=0;

    1.0158 0.0129 0.0389 0.0129 0.0129 0.0065
    0.9652 0.0005 0.1021 0.0195 0.0183 (-0.0057)
    0.9000 0.0000 0.5 0.5 0.1000 0.5
    0.9673 0.0327 0.5 0.5 0.5 0.5
    0.8954 0.0856 0.0095 0.5 0.0095 0.5
      (0) 0.0035 0.0035 0.0035 0.0035 0.0035


"Roger Stafford" wrote in message <jei5r8$7ar$1@newscl01ah.mathworks.com>...
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jeh0mh$1l4$1@newscl01ah.mathworks.com>...
> > can you tell me what to do exactly
> > do you mean A(A(1:m,:) < 0)=eps
> > i use 1:m because i update row by row in this matrix
> >
> > i 've 50 iterations and this error appears in 20th or 30th iterations
> >
> > i mean setting -ve values to 0 is ok in some iterations but the problem appears later iterations so it gives me wrong result
> >
> > "ScottB" wrote in message <jefrus$g1s$1@newscl01ah.mathworks.com>...
> > > You're getting floating point representations of zero. You can assume values around eps to be zero (set them). Take a look at the command "eps".
> > >
> > > ScottB
> - - - - - - - - -
> There is something very wrong with your computations. You should be getting exact zeros wherever you have an assignment indicated by "= 0" and exact ones with "= 1". There is no question of a floating point representation of these integer values being anything other than exactly correct.
>
> I think you had better show us more of the details of your procedure. You say "i update row by row in this matrix", but writing
>
> A(A(1:m,:) < 0)=0;
>
> is not a row by row process. It deals with all rows from 1 to m together.
>
> Moreover, if the value of m is allowed to be anything other than size(A,1), you will surely get very erroneous results. This is also true with your use of the 'find' function. At best you would have the wrong elements being indexed.
>
> Please show us precisely what you are doing. If it is placed within a loop show us the whole loop.
>
> Roger Stafford

Subject: matlab refuse to set matrix values to 0

From: dpb

Date: 11 Jan, 2012 13:51:58

Message: 7 of 14

On 1/11/2012 4:49 AM, besbesmany besbesmany wrote:
> i found the problem as follows
> it takes the index of -ve value in wrong way
> i mean if i want to make A(2,6)=0 it makes A(12)=0

Precisely. The indices 2,6 and 12(,1) are the identical position.
Nothing wrong at all other than your perception and/or expectations (the
latter of which I can't decipher).

> i mean A(A(1:2,:) < 0)=0; it understand it as A(12,1)=0 and not A(2,6)=0

...

A(1:2,:) is a subsection of A consisting of the first two rows and all
columns. Wherever a entry in A in that subset exists that is 0, it will
be replaced with zero.

Wherever that entry is in the larger complete array A is obviously
different linear index than it is in the subset array.

I am unable to parse the rest well enough to understand what you are
trying to do that the expression as written doesn't accomplish. If you
want to address a 2D array alternatively as linear or as doubly
subscripted, see

doc ind2sub
doc sub2ind

for the translation functions. But, you _MUST_ be referring to the same
size and/or section of an array for the translation to make any
sense--in your above you're mixing the full array and a sub-array.

--

Subject: matlab refuse to set matrix values to 0

From: besbesmany besbesmany

Date: 11 Jan, 2012 15:46:08

Message: 8 of 14

is there a short code than this one

for ro=1:m
    for gg = 1:col
        if (A(ro,col) < 0)
            A(ro,col) = 0;
         end
    end
end

to save processing time
i'm caring only about m but columns can be :
like this A(1:m ,:)

dpb <none@non.net> wrote in message <jek45v$lkn$1@speranza.aioe.org>...
> On 1/11/2012 4:49 AM, besbesmany besbesmany wrote:
> > i found the problem as follows
> > it takes the index of -ve value in wrong way
> > i mean if i want to make A(2,6)=0 it makes A(12)=0
>
> Precisely. The indices 2,6 and 12(,1) are the identical position.
> Nothing wrong at all other than your perception and/or expectations (the
> latter of which I can't decipher).
>
> > i mean A(A(1:2,:) < 0)=0; it understand it as A(12,1)=0 and not A(2,6)=0
>
> ...
>
> A(1:2,:) is a subsection of A consisting of the first two rows and all
> columns. Wherever a entry in A in that subset exists that is 0, it will
> be replaced with zero.
>
> Wherever that entry is in the larger complete array A is obviously
> different linear index than it is in the subset array.
>
> I am unable to parse the rest well enough to understand what you are
> trying to do that the expression as written doesn't accomplish. If you
> want to address a 2D array alternatively as linear or as doubly
> subscripted, see
>
> doc ind2sub
> doc sub2ind
>
> for the translation functions. But, you _MUST_ be referring to the same
> size and/or section of an array for the translation to make any
> sense--in your above you're mixing the full array and a sub-array.
>
> --

Subject: matlab refuse to set matrix values to 0

From: dpb

Date: 11 Jan, 2012 15:57:28

Message: 9 of 14

On 1/11/2012 9:46 AM, besbesmany besbesmany wrote:
> is there a short code than this one
> for ro=1:m
> for gg = 1:col
> if (A(ro,col) < 0)
> A(ro,col) = 0;
> end
> end
> end
>
> to save processing time i'm caring only about m but columns can be :
> like this A(1:m ,:)

Presuming m and col are a set of upper indices >1 and within the bounds
given by size(A), the loops above are equivalent to

A(A(1:m,1:col)<0)=0;

A(1:m,1:col) is a 2D subarray of A consisting of m rows and col columns.

--

Subject: matlab refuse to set matrix values to 0

From: besbesmany besbesmany

Date: 11 Jan, 2012 16:43:08

Message: 10 of 14

please can you give me the code
or ro=1:m
    for gg = 1:col
        if (A(ro,col) < 0)
            A(ro,col) = 0;
         end
    end
end

in 1 or 2 lines using ind2sub or similar function

the problem that i update matrix from 1:m with iterations so m is differe from loop to another
when i use your code
A(A(1:m,1:col)<0)=0;
it give me error

like this where m=4 for certain loop
A=rand(5)
 A(A(1:4,1:5)>0.5)=0
it give me wrong indexs becomes 0


dpb <none@non.net> wrote in message <jekbhb$7qs$1@speranza.aioe.org>...
> On 1/11/2012 9:46 AM, besbesmany besbesmany wrote:
> > is there a short code than this one
> > for ro=1:m
> > for gg = 1:col
> > if (A(ro,col) < 0)
> > A(ro,col) = 0;
> > end
> > end
> > end
> >
> > to save processing time i'm caring only about m but columns can be :
> > like this A(1:m ,:)
>
> Presuming m and col are a set of upper indices >1 and within the bounds
> given by size(A), the loops above are equivalent to
>
> A(A(1:m,1:col)<0)=0;
>
> A(1:m,1:col) is a 2D subarray of A consisting of m rows and col columns.
>
> --

Subject: matlab refuse to set matrix values to 0

From: dpb

Date: 11 Jan, 2012 18:12:28

Message: 11 of 14

On 1/11/2012 10:43 AM, besbesmany besbesmany wrote:
...

> the problem that i update matrix from 1:m with iterations so m is
> differe from loop to another when i use your code
> A(A(1:m,1:col)<0)=0;
> it give me error
> like this where m=4 for certain loop
> A=rand(5)
> A(A(1:4,1:5)>0.5)=0
> it give me wrong indexs becomes 0
...

I have no clue what you're driving at...

We need a _complete_ (but short) example that demonstrates what you find
to be a wrong index. So far, I've not seen anything that makes me think
that

 > A=rand(5)
 > A(A(1:4,1:5)>0.5)=0
 > it give me wrong indexs becomes 0

Show the modification in the loop; so far there's been nothing shown but
a static array.

--

Subject: matlab refuse to set matrix values to 0

From: Roger Stafford

Date: 11 Jan, 2012 18:46:08

Message: 12 of 14

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jekas0$97h$1@newscl01ah.mathworks.com>...
> is there a short code than this one
>
> for ro=1:m
> for gg = 1:col
> if (A(ro,col) < 0)
> A(ro,col) = 0;
> end
> end
> end
- - - - - - - - - -
  If m < size(A,1), then the following should do what you want:

 [I,J] = find(A(1:m,1:col)<0);
 A(sub2ind(size(A),I,J)) = 0;

  You cannot directly use the logical indexing from A(1:m,1:col)<0 because it is based on a number of rows equal to m, not size(A,1).

  (I assume you meant to write:

 for ro=1:m
  for gg = 1:col
   if (A(ro,gg) < 0)
    A(ro,gg) = 0;
   end
  end
 end )

Roger Stafford

Subject: matlab refuse to set matrix values to 0

From: dpb

Date: 11 Jan, 2012 18:59:17

Message: 13 of 14

On 1/11/2012 12:46 PM, Roger Stafford wrote:
...

> [I,J] = find(A(1:m,1:col)<0);
> A(sub2ind(size(A),I,J)) = 0;
>
> You cannot directly use the logical indexing from A(1:m,1:col)<0 because
> it is based on a number of rows equal to m, not size(A,1).
...

Ah..._finally_ dawns on me what OP was after/where his problem lay.

I couldn't see the forest for the difficulty in parsing... :(

--

Subject: matlab refuse to set matrix values to 0

From: besbesmany besbesmany

Date: 11 Jan, 2012 21:45:09

Message: 14 of 14

Yes Yes Yes
that's what i want Roger
 [I,J] = find(A(1:m,1:col)<0);
 A(sub2ind(size(A),I,J)) = 0;

and i made columns : and give me right results too
 [I,J] = find(A(1:m,:)<0);

really thanks alot for all who helped me :)))

Roger Stafford
dpb
ScottB
James Tursa


if you know about conditional breakpoint , please help me in this link

http://www.mathworks.com/matlabcentral/newsreader/view_thread/315876

i want to put dbstop and dbclear
inside the parsing code for certain condition
ismember(1,isnan(A(1:m,:)))


"Roger Stafford" wrote in message <jekldg$h3b$1@newscl01ah.mathworks.com>...
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jekas0$97h$1@newscl01ah.mathworks.com>...
> > is there a short code than this one
> >
> > for ro=1:m
> > for gg = 1:col
> > if (A(ro,col) < 0)
> > A(ro,col) = 0;
> > end
> > end
> > end
> - - - - - - - - - -
> If m < size(A,1), then the following should do what you want:
>
> [I,J] = find(A(1:m,1:col)<0);
> A(sub2ind(size(A),I,J)) = 0;
>
> You cannot directly use the logical indexing from A(1:m,1:col)<0 because it is based on a number of rows equal to m, not size(A,1).
>
> (I assume you meant to write:
>
> for ro=1:m
> for gg = 1:col
> if (A(ro,gg) < 0)
> A(ro,gg) = 0;
> end
> end
> end )
>
> Roger Stafford

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