Thread Subject: Quick question about changing matrix values

Subject: Quick question about changing matrix values

From: Matthew Allen

Date: 14 Apr, 2008 17:37:01

Message: 1 of 7

Hi,

I want to know if there is a way to set all matrix values
between say 0 and 10 to 1.

EX I have a matrix of [-1 2 3 11 12 18]

and I want to set all alues between 1 and 10 to 1 so that I
end up with a matrix of
[-1 1 1 11 12 18]

Subject: Quick question about changing matrix values

From: French Caro

Date: 14 Apr, 2008 17:49:15

Message: 2 of 7

"Matthew Allen" <allenmatthew@yahoo.com> wrote in message
<fu04nt$6c$1@fred.mathworks.com>...
> Hi,
>
> I want to know if there is a way to set all matrix values
> between say 0 and 10 to 1.
>
> EX I have a matrix of [-1 2 3 11 12 18]
>
> and I want to set all alues between 1 and 10 to 1 so that I
> end up with a matrix of
> [-1 1 1 11 12 18]
>
>
Use logical indexing :
A=[-1 2 3 11 12 18]
A(A>0 & A<11)=1

Subject: Quick question about changing matrix values

From: Matthew Allen

Date: 14 Apr, 2008 18:01:03

Message: 3 of 7

"French Caro " <caro95470@nospam.free.fr> wrote in message
<fu05er$fmc$1@fred.mathworks.com>...
> "Matthew Allen" <allenmatthew@yahoo.com> wrote in message
> <fu04nt$6c$1@fred.mathworks.com>...
> > Hi,
> >
> > I want to know if there is a way to set all matrix
values
> > between say 0 and 10 to 1.
> >
> > EX I have a matrix of [-1 2 3 11 12 18]
> >
> > and I want to set all alues between 1 and 10 to 1 so
that I
> > end up with a matrix of
> > [-1 1 1 11 12 18]
> >
> >
> Use logical indexing :
> A=[-1 2 3 11 12 18]
> A(A>0 & A<11)=1


Thanks thats perfect. I didn't know you could use the &

Subject: Quick question about changing matrix values

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

Date: 14 Apr, 2008 18:06:18

Message: 4 of 7

In article <fu05er$fmc$1@fred.mathworks.com>,
French Caro <caro95470@nospam.free.fr> wrote:
>"Matthew Allen" <allenmatthew@yahoo.com> wrote in message
><fu04nt$6c$1@fred.mathworks.com>...

>> I want to know if there is a way to set all matrix values
>> between say 0 and 10 to 1.

>> EX I have a matrix of [-1 2 3 11 12 18]

>> and I want to set all alues between 1 and 10 to 1 so that I
>> end up with a matrix of
>> [-1 1 1 11 12 18]

>Use logical indexing :
>A=[-1 2 3 11 12 18]
>A(A>0 & A<11)=1

The first requirement stated was "between 0 and 10"; then it was
stated as "between 1 and 10". The example does not provide us with
any guidance as to what to do for values that are -exactly- 0 or 1,
or -exactly- 10 -- is the interval "open" or "closed"?

The suggested solution A(A>0 & A<11)=1 is suitable for [1,10] (closed)
provided that the values are all integers, but is not suitable
for [0,10] if the numbers are integers or real, and is not suitable
for any of the range interpretations if the numbers are real -- for
example, if 10.37134 is a possibility, then that value would be set to 1
even though it is not "between" 0 and 10 or 1 and 10.

Clarification from the original poster as to the intent of "between"
and the domain of values would be useful. Are the array values calculated?
If so, then could one of them end up being 1-eps(1), and if so should
that be in the range or out of it?
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: Quick question about changing matrix values

From: Abel Brown

Date: 15 Apr, 2008 18:02:02

Message: 5 of 7

"Matthew Allen" <allenmatthew@yahoo.com> wrote in message
<fu064v$orb$1@fred.mathworks.com>...
> "French Caro " <caro95470@nospam.free.fr> wrote in message
> <fu05er$fmc$1@fred.mathworks.com>...
> > "Matthew Allen" <allenmatthew@yahoo.com> wrote in message
> > <fu04nt$6c$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > I want to know if there is a way to set all matrix
> values
> > > between say 0 and 10 to 1.
> > >
> > > EX I have a matrix of [-1 2 3 11 12 18]
> > >
> > > and I want to set all alues between 1 and 10 to 1 so
> that I
> > > end up with a matrix of
> > > [-1 1 1 11 12 18]
> > >
> > >
> > Use logical indexing :
> > A=[-1 2 3 11 12 18]
> > A(A>0 & A<11)=1
>
>
> Thanks thats perfect. I didn't know you could use the &


A less effective and more cumbersome solution would be to
use the "find" function to FIND the indx of the values
(1-10) then go throu the indx and set the values to 1.

example:

 % something like ...
 [n,m] = find(A <=10 & A >=1);
 A(n,m) = 1;



 etc etc....

Subject: Quick question about changing matrix values

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

Date: 15 Apr, 2008 18:10:04

Message: 6 of 7

In article <fu2qiq$bq5$1@fred.mathworks.com>,
Abel Brown <brown.2179@osu.edu> wrote:

>A less effective and more cumbersome solution would be to
>use the "find" function to FIND the indx of the values
>(1-10) then go throu the indx and set the values to 1.

>example:

> % something like ...
> [n,m] = find(A <=10 & A >=1);
> A(n,m) = 1;

If A is not a row or column vector (that is, it is a 2D matrix),
then that solution would set the wronng locations, as it would
set 1 at every location (p,q) where p is an element of n and
q is an element of m, rather than only setting 1 at locations
[n(1),m(1)], [n(2),m(2)], [n(3),m(3)] and so on.
--
  "I will not approve any plan which is based on the old principle
  of build now and repair later." -- Walter Hickle

Subject: Quick question about changing matrix values

From: Abel Brown

Date: 15 Apr, 2008 18:23:28

Message: 7 of 7

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu2r1s$kr5$1@canopus.cc.umanitoba.ca>...
> In article <fu2qiq$bq5$1@fred.mathworks.com>,
> Abel Brown <brown.2179@osu.edu> wrote:
>
> >A less effective and more cumbersome solution would be to
> >use the "find" function to FIND the indx of the values
> >(1-10) then go throu the indx and set the values to 1.
>
> >example:
>
> > % something like ...
> > [n,m] = find(A <=10 & A >=1);
> > A(n,m) = 1;
>
> If A is not a row or column vector (that is, it is a 2D
matrix),
> then that solution would set the wronng locations, as it would
> set 1 at every location (p,q) where p is an element of n and
> q is an element of m, rather than only setting 1 at locations
> [n(1),m(1)], [n(2),m(2)], [n(3),m(3)] and so on.
> --
> "I will not approve any plan which is based on the old
principle
> of build now and repair later." --
Walter Hickle

yes yes, but the logic is the same ... that's why it says
"something like ..."

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

Contact us at files@mathworks.com