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:
array question (fairly simple)

Subject: array question (fairly simple)

From: Mohit Thukral

Date: 13 Jan, 2011 15:32:06

Message: 1 of 8

Hi guys got an array question.

Conside the array

(1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)

Ok now I want to make an array from this such that:

If the price movement is >3 I would get a 1 output
If the price movement is <3 I would get a 2 output
If else I would get 3

So for my example I would get
(1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)

Please help!

Subject: array question (fairly simple)

From: Sean de

Date: 13 Jan, 2011 15:42:05

Message: 2 of 8

"Mohit Thukral" wrote in message <ign5tm$37t$1@fred.mathworks.com>...
> Hi guys got an array question.
>
> Conside the array
>
> (1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)
>
> Ok now I want to make an array from this such that:
>
> If the price movement is >3 I would get a 1 output
> If the price movement is <3 I would get a 2 output
> If else I would get 3
>
> So for my example I would get
> (1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)
>
> Please help!

doc diff

Subject: array question (fairly simple)

From: Mohit Thukral

Date: 13 Jan, 2011 15:49:04

Message: 3 of 8

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ign6gd$and$1@fred.mathworks.com>...
> "Mohit Thukral" wrote in message <ign5tm$37t$1@fred.mathworks.com>...
> > Hi guys got an array question.
> >
> > Conside the array
> >
> > (1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)
> >
> > Ok now I want to make an array from this such that:
> >
> > If the price movement is >3 I would get a 1 output
> > If the price movement is <3 I would get a 2 output
> > If else I would get 3
> >
> > So for my example I would get
> > (1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)
> >
> > Please help!
>
> doc diff

Yes O troed difference however once I have diference it is easy for me to get 1 and 2 outputs but when I do 3 output it makes em all 3. Could someone please code the example I gave above

Subject: array question (fairly simple)

From: Mohit Thukral

Date: 13 Jan, 2011 15:56:05

Message: 4 of 8

"Mohit Thukral" wrote in message <ign6tg$s1k$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ign6gd$and$1@fred.mathworks.com>...
> > "Mohit Thukral" wrote in message <ign5tm$37t$1@fred.mathworks.com>...
> > > Hi guys got an array question.
> > >
> > > Conside the array
> > >
> > > (1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)
> > >
> > > Ok now I want to make an array from this such that:
> > >
> > > If the price movement is >3 I would get a 1 output
> > > If the price movement is <3 I would get a 2 output
> > > If else I would get 3
> > >
> > > So for my example I would get
> > > (1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)
> > >
> > > Please help!
> >
> > doc diff
>
> Yes O troed difference however once I have diference it is easy for me to get 1 and 2 outputs but when I do 3 output it makes em all 3. Could someone please code the example I gave above

Could anyone solve the problem please?

Best wishes,

Subject: array question (fairly simple)

From: Mate 2u

Date: 13 Jan, 2011 16:34:06

Message: 5 of 8

"Mate 2u" wrote in message <ign7al$6df$1@fred.mathworks.com>...
> "Mohit Thukral" wrote in message <ign6tg$s1k$1@fred.mathworks.com>...
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ign6gd$and$1@fred.mathworks.com>...
> > > "Mohit Thukral" wrote in message <ign5tm$37t$1@fred.mathworks.com>...
> > > > Hi guys got an array question.
> > > >
> > > > Conside the array
> > > >
> > > > (1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)
> > > >
> > > > Ok now I want to make an array from this such that:
> > > >
> > > > If the price movement is >3 I would get a 1 output
> > > > If the price movement is <3 I would get a 2 output
> > > > If else I would get 3
> > > >
> > > > So for my example I would get
> > > > (1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)
> > > >
> > > > Please help!
> > >
> > > doc diff
> >
> > Yes O troed difference however once I have diference it is easy for me to get 1 and 2 outputs but when I do 3 output it makes em all 3. Could someone please code the example I gave above
>
> Could anyone solve the problem please?
>
> Best wishes,

?

Subject: array question (fairly simple)

From: I am too much me

Date: 13 Jan, 2011 16:50:49

Message: 6 of 8

On 13/01/11 10:34 AM, Mate 2u wrote:
> "Mate 2u" wrote in message <ign7al$6df$1@fred.mathworks.com>...
>> "Mohit Thukral" wrote in message <ign6tg$s1k$1@fred.mathworks.com>...
>> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in
>> message <ign6gd$and$1@fred.mathworks.com>...
>> > > "Mohit Thukral" wrote in message <ign5tm$37t$1@fred.mathworks.com>...
>> > > > Hi guys got an array question.
>> > > > > > > Conside the array
>> > > > > > > (1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)
>> > > > > > > Ok now I want to make an array from this such that:
>> > > > > > > If the price movement is >3 I would get a 1 output
>> > > > If the price movement is <3 I would get a 2 output
>> > > > If else I would get 3
>> > > > > > > So for my example I would get > > > (1, 3, 3, 3, 3, 3, 3,
>> 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)
>> > > > > > > Please help!
>> > > > > doc diff
>> > > Yes O troed difference however once I have diference it is easy
>> for me to get 1 and 2 outputs but when I do 3 output it makes em all
>> 3. Could someone please code the example I gave above
>>
>> Could anyone solve the problem please?
>>
>> Best wishes,
>
> ?

Wait your turn. People who posted before you have moral priority for
being answered before you.

d = [diff(A(:)); nan];
r = zeros(size(A));
r(d<3) = 1;
r(d>3) = 2;
r(d==3) = 3;

Subject: array question (fairly simple)

From: Susan

Date: 13 Jan, 2011 17:55:08

Message: 7 of 8

I am too much me <roberson@hushmail.com> wrote in message <J3GXo.10943$111.6388@newsfe12.iad>...
> On 13/01/11 10:34 AM, Mate 2u wrote:
> > "Mate 2u" wrote in message <ign7al$6df$1@fred.mathworks.com>...
> >> "Mohit Thukral" wrote in message <ign6tg$s1k$1@fred.mathworks.com>...
> >> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in
> >> message <ign6gd$and$1@fred.mathworks.com>...
> >> > > "Mohit Thukral" wrote in message <ign5tm$37t$1@fred.mathworks.com>...
> >> > > > Hi guys got an array question.
> >> > > > > > > Conside the array
> >> > > > > > > (1,5,3,4,4,3,6,3,2,5,5,8,3,9,4,3,6,3,7)
> >> > > > > > > Ok now I want to make an array from this such that:
> >> > > > > > > If the price movement is >3 I would get a 1 output
> >> > > > If the price movement is <3 I would get a 2 output
> >> > > > If else I would get 3
> >> > > > > > > So for my example I would get > > > (1, 3, 3, 3, 3, 3, 3,
> >> 3, 3, 3, 3, 2, 1, 2, 3, 3, 3, 3, 1)
> >> > > > > > > Please help!
> >> > > > > doc diff
> >> > > Yes O troed difference however once I have diference it is easy
> >> for me to get 1 and 2 outputs but when I do 3 output it makes em all
> >> 3. Could someone please code the example I gave above
> >>
> >> Could anyone solve the problem please?
> >>
> >> Best wishes,
> >
> > ?
>
> Wait your turn. People who posted before you have moral priority for
> being answered before you.
>
> d = [diff(A(:)); nan];
> r = zeros(size(A));
> r(d<3) = 1;
> r(d>3) = 2;
> r(d==3) = 3;

Actually, you want
d = [diff(A(:)); nan];
r = zeros(size(A))+3; % start with all 3s
% since 3 is the most common result (all diffs >=-3 and <=+3)
r(d>3) = 1; % increases more than 3
r(d<-3) = 2; % decreases more than 3
r = r(1:end-1); % remove the last nan diff

Subject: array question (fairly simple)

From: I am too much me

Date: 13 Jan, 2011 18:09:53

Message: 8 of 8

On 13/01/11 11:55 AM, Susan wrote:
> I am too much me <roberson@hushmail.com> wrote in message

>> d = [diff(A(:)); nan];
>> r = zeros(size(A));
>> r(d<3) = 1;
>> r(d>3) = 2;
>> r(d==3) = 3;

> Actually, you want
> d = [diff(A(:)); nan];
> r = zeros(size(A))+3; % start with all 3s
> % since 3 is the most common result (all diffs >=-3 and <=+3)
> r(d>3) = 1; % increases more than 3
> r(d<-3) = 2; % decreases more than 3
> r = r(1:end-1); % remove the last nan diff

That is simply an optimization except for the part about the training
nan. I wanted to write as clearly as possible as the original poster is
having trouble with some basics.

Thinking about it a moment more, I realized that in all places that we
referenced d, we should have used abs(d), as in

d = [diff(A(:)); nan];
r = zeros(size(A));
r(abs(d)<3) = 1;
r(abs(d)>3) = 2;
r(abs(d)==3) = 3;


The trailing nan leads to the interesting question of how long the
output is expected to be, and if the expected length of the output is to
be the same as the length of the input, then what value to use for
either the first or last value.

I went back to the original example of the poster, and I found that the
length of the output was the same as the length of the input. On the
other hand I also found that the output given was a nearly complete
mismatch for the rules that were given, with many 3's appearing in the
output in cases in which the difference between the elements was not
exactly 3. I was thus unable to determine the rule to be used for the
final output... unless perhaps we consider the case of having no price
to compare against to fall within "otherwise".

Tags for 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