Thread Subject: Finding specific values in a vector and subtracting 10.

Subject: Finding specific values in a vector and subtracting 10.

From: John Gage

Date: 22 Mar, 2010 04:06:09

Message: 1 of 3

Hi, just need a little help, I have no idea how to go about this.

If i was given an arbitrarily sized row vector, [1 18 4 5 9 14 11] in
this case. How would I identify all the values that are bigger than 9,
subtract 10 from them and add 1 onto the previous value in the row?
Thanks =)

Subject: Finding specific values in a vector and subtracting 10.

From: Walter Roberson

Date: 22 Mar, 2010 04:54:57

Message: 2 of 3

John Gage wrote:
> Hi, just need a little help, I have no idea how to go about this.
>
> If i was given an arbitrarily sized row vector, [1 18 4 5 9 14 11] in
> this case. How would I identify all the values that are bigger than 9,
> subtract 10 from them and add 1 onto the previous value in the row?

Ambiguous. Suppose you had [9 10 11]. Then subtracting 10 from the
elements > 9 would give [9 0 1]. Adding 1 to the previous value in the
row would give [10 1 0], or alternately with your specification could
give [10 11 0]. The ambiguity is that when you say "previous value in
the row", you might mean "the value at the previous index location after
the subtractions have been done", but since you did not specify
sequential processing, it would be equally valid to hold on to the
original value at that index location and increment that.

Also, what should be done if the element > 9 occurs in the first position?

And is it really okay that a value could end up at 10 -- e.g., in your
example, you have 9 14 so the 9 would be incremented to 10 and the 14
would go to 4. Is that right, or should it trickle left so that the [5 9
14] would go to [6 0 4], leaving no value > 9 ?


Sequential code:

T = A>9;
A(T) = A(T) - 10;
A([T(2:end) false]) = A([T(2:end) false])+ 1;


Alternative code permitted by specification:

A([find(A>9) find(A(2:end)>9]) = [A(A>9)-10 A(A(2:end)>9) + 1];

Subject: Finding specific values in a vector and subtracting 10.

From: Jack Daniels

Date: 22 Mar, 2010 09:07:04

Message: 3 of 3

Walter Roberson <roberson@hushmail.com> wrote in message <ho6t72$k3i$1@canopus.cc.umanitoba.ca>...
> Ambiguous. Suppose you had [9 10 11]. Then subtracting 10 from the
> elements > 9 would give [9 0 1]. Adding 1 to the previous value in the
> row would give [10 1 0], or alternately with your specification could
> give [10 11 0]. The ambiguity is that when you say "previous value in
> the row", you might mean "the value at the previous index location after
> the subtractions have been done", but since you did not specify
> sequential processing, it would be equally valid to hold on to the
> original value at that index location and increment that.
>
> Also, what should be done if the element > 9 occurs in the first position?
>
> And is it really okay that a value could end up at 10 -- e.g., in your
> example, you have 9 14 so the 9 would be incremented to 10 and the 14
> would go to 4. Is that right, or should it trickle left so that the [5 9
> 14] would go to [6 0 4], leaving no value > 9 ?
>
>
> Sequential code:
>
> T = A>9;
> A(T) = A(T) - 10;
> A([T(2:end) false]) = A([T(2:end) false])+ 1;
>
>
> Alternative code permitted by specification:
>
> A([find(A>9) find(A(2:end)>9]) = [A(A>9)-10 A(A(2:end)>9) + 1];

Sorry for the ambiguity, firstly yes I did mean "the value at the previous index location after the subtractions have been done". And yes it should trickle left leaving no value > 9, if a value >9 occurs in the first position then, 10 should be subtracted from it, the whole row vector should be shifted right by 1, essentially adding an extra column, leaving the value in the first position as 1.

For Example:

[9 11 2 3 4] would become
[1 0 1 2 3 4]

or [11 12 5 13 7] would become
[1 2 2 6 3 7]

So the sequential code you provided is correct, it just doesn't account for the first value being >9 as i would like it to. Could it be adjusted at all?

Thanks for your help

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