Thread Subject: shifting data within a vector

Subject: shifting data within a vector

From: Hydroman S

Date: 5 May, 2008 22:58:03

Message: 1 of 4

For a vector s, I found its maximum value, and located it

s=rand(20,1);
max(s);
find(s==max(s));


Now I want to add a value of say x=10; to the max(s) and
place it in the following row(raw after max(s)), then add
a value of 20 to the max(s)and add it to the following
raw....etc till the end raw?

Any suggestions?

Subject: shifting data within a vector

From: Roger Stafford

Date: 5 May, 2008 23:28:04

Message: 2 of 4

"Hydroman S" <amirgsalem@gmail.com> wrote in message <fvo3dr$8qk
$1@fred.mathworks.com>...
> For a vector s, I found its maximum value, and located it
>
> s=rand(20,1);
> max(s);
> find(s==max(s));
>
>
> Now I want to add a value of say x=10; to the max(s) and
> place it in the following row(raw after max(s)), then add
> a value of 20 to the max(s)and add it to the following
> raw....etc till the end raw?
>
> Any suggestions?
-----------
  First of all, you don't need 'find' here. Just use the index returned by max:

 [mx,ix] = max(s); % ix is the index of the maximum element of x

  The rest of what you are asking is rather hard to interpret. Your misuse of
the word 'raw' is confusing! My guess is that, starting with ix+1, you wish to
place mx+10, mx+20, ..., in ix+1, ix+2, etc., on to the end of s. Is that it?

 s(ix+1:end) = mx+(10:10:10*(length(s)-ix))';

Roger Stafford

Subject: shifting data within a vector

From: Roger Stafford

Date: 5 May, 2008 23:35:00

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fvo564$i8p$1@fred.mathworks.com>...
> First of all, you don't need 'find' here. Just use the index returned by max:
>
> [mx,ix] = max(s); % ix is the index of the maximum element of x
>
> The rest of what you are asking is rather hard to interpret. Your misuse
of
> the word 'raw' is confusing! My guess is that, starting with ix+1, you wish
to
> place mx+10, mx+20, ..., in ix+1, ix+2, etc., on to the end of s. Is that it?
>
> s(ix+1:end) = mx+(10:10:10*(length(s)-ix))';
>
> Roger Stafford
-------
  After looking at your wordage again, it seems possible that what you
actually want is this:

 [mx,ix] = max(s); % ix is the index of the maximum element of x
 s(ix+1:end) = s(ix+1:end) + mx+(10:10:10*(length(s)-ix))';

Roger Stafford

Subject: shifting data within a vector

From: Hydroman S

Date: 6 May, 2008 03:38:03

Message: 4 of 4

Thank you Roger. Your first reply answers my question.
Apologies for the ‘raw’ stuff, sometimes this stuff comes
from the gut instead of the brain.

Here is exactly what I wanted to do, and thanks again for
your help:

s=rand(20,1);
[mx,ix] = max(s); % ix is the index of the maximum element
of x
s(ix+1:end) = mx+(10:10:10*(length(s)-ix))';
s(1:ix-1)= mx-(10:10:10*(ix-1))';

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