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:
Inserting/Pushing a New Value into a Vector

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 3 Sep, 2013 14:04:09

Message: 1 of 19

Hi all,

Here's a sample vector, Vec = [6, 2 , 1 , 6 , 5 , 2 , 1 , 5 , 3 , 1 , 5 , 4 , 1 , 6 , 4 , 1];
I'd like to able to add, NewVal = 6, straight after the occurance of element "1", and if "6" already exists after it, then do nothing. Basically, the output I'm looking for is somehting like this:

Output, NewVec = 6 2 1 6 5 2 1 6 5 3 1 6 5 4 1 6 4 1;

I've tried all the tips on-line;
__________________________________________________________________
First Attempt:
clc; clear
PathNow = [6,2,1,6,5,2,1,5,3,1,5,4,1,6,4,1];
FirstTask = 1;
Unique_tIDs = PathNow(1,1);
LocFirstTask = find(PathNow==FirstTask);
LocFirstTask = LocFirstTask(1:end-1);
for i = 1:length(LocFirstTask)
    LocFirstTask = find(PathNow==FirstTask);
    LocFirstTask = LocFirstTask(1:end-1);
    NewRecord = [diag(kron(PathNow.',ones(1,LocFirstTask(i))));Unique_tIDs;flipud(diag(kron(flipud(PathNow.'),ones(1,length(PathNow)-LocFirstTask(i)))))].'
end

The output here was:
6 2 1 6 5 2 1 5 3 1 5 4 1 6 6 4 1
__________________________________________________________________
Second Attempt:
clc; clear
PathNow = [6,2,1,6,5,2,1,5,3,1,5,4,1,6,4,1];
Unique_tIDs = PathNow(1, 1);
FirstTask = 1;
LocFirstTask = find(PathNow==FirstTask);
LocFirstTask = LocFirstTask(1:end-1);
c=false(1, length(PathNow)+length(LocFirstTask));
c(LocFirstTask)=true;
NewRecord = nan(size(c));
NewRecord(~c) = PathNow;
NewRecord(c) = Unique_tIDs

The output was:
6 2 6 1 6 5 6 2 1 6 5 3 6 1 5 4 1 6 4 1
__________________________________________________________________
Third, and final, Attempt:
clc; clear
PathNow = [6,2,1,6,5,2,1,5,3,1,5,4,1,6,4,1]; % A
FirstTask = 1;
Unique_tIDs = 6; % B
Unique_tIDs_i = find(PathNow==FirstTask); % Bi
Unique_tIDs_i = Unique_tIDs_i(1:end-1)
PathNowNew = zeros(1, length(PathNow) + length(Unique_tIDs_i)) + NaN; % or Unique_tIDs_i
PathNowNew(Unique_tIDs_i+length(Unique_tIDs_i)-1) = Unique_tIDs;
PathNowNew(isnan(PathNowNew)) = PathNow

The two outputs were:
PathNow: 6 2 1 6 5 2 1 5 3 1 5 4 1 6 4 1
PathNowNew: 6 2 1 6 5 6 2 1 5 6 3 1 6 5 4 6 1 6 4 1

Any tips would be much appreciated, ta.

Subject: Inserting/Pushing a New Value into a Vector

From: Steven_Lord

Date: 3 Sep, 2013 14:52:35

Message: 2 of 19



"Tiger " <h4ghazzawi@hotmail.co.uk> wrote in message
news:l04q8p$ivg$1@newscl01ah.mathworks.com...
> Hi all,
>
> Here's a sample vector, Vec = [6, 2 , 1 , 6 , 5 , 2 , 1 , 5 , 3 , 1 , 5 ,
> 4 , 1 , 6 , 4 , 1];
> I'd like to able to add, NewVal = 6, straight after the occurance of
> element "1", and if "6" already exists after it, then do nothing.
> Basically, the output I'm looking for is somehting like this:

v = 1:10;
if v(5) ~= 6
    v = [v(1:5) 17 v(6:end)];
end
v

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Inserting/Pushing a New Value into a Vector

From: dpb

Date: 3 Sep, 2013 14:53:17

Message: 3 of 19

On 9/3/2013 9:04 AM, Tiger wrote:
...
> ... Vec = [6 2 1 6 5 2 1 5 3 1 5 4 1 6 4 1];
> I'd like to able to add, NewVal = 6, straight after the occurance of
> element "1", and if "6" already exists after it, then do nothing.
> Basically, the output I'm looking for is somehting like this:
>
> Output, NewVec = 6 2 1 6 5 2 1 6 5 3 1 6 5 4 1 6 4 1;
>
...

Straightahead solution...

 >> ix=(v==1)&[diff(v)~=5 0];
 >> V=num2cell(v);
 >> V(ix)={[1 6]};
 >> V=cell2mat(V)
V =
   6 2 1 6 5 2 1 6 5 3 1 6 5 4 1 6 4 1
 >>

Can be cleaned up, obviously...

--

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 3 Sep, 2013 15:29:08

Message: 4 of 19

Hi dpb,

Thanks for your quick response. Is this a dynamic way of doing it i.e. given any size of a vector, I would like to locate the "1s" and see if the element I want to insert already exists there before actually assigning it.

For instance, if I have a vector =
6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1

Basically, I'd like to see where all the "1s" occur and whether the first value of the vector i.e. "6" falls right after each one of them, if not, then I want to add it.

The output I'm trying to achieve is:
6 2 1 6 4 1 6 5 2 1 6 5 3 1 6 5 4 1... and there are other vector sizes I'd like to manipulate, however the principle of searching for the "1s" and ainserting a particular value remains the same throughout the various vector sizes.

How's that possible. Many thanks indeed, ta.

Subject: Inserting/Pushing a New Value into a Vector

From: dpb

Date: 3 Sep, 2013 17:43:26

Message: 5 of 19

On 9/3/2013 10:29 AM, Tiger wrote:

...[Please don't toppost--hard conversation follow makes]...

> Hi dpb,
>
> Thanks for your quick response. Is this a dynamic way of doing it i.e.
> given any size of a vector, I would like to locate the "1s" and see if
> the element I want to insert already exists there before actually
> assigning it.
>
> For instance, if I have a vector =
> 6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1
>
> Basically, I'd like to see where all the "1s" occur and whether the
> first value of the vector i.e. "6" falls right after each one of them,
> if not, then I want to add it.
>
...

Sure, read up on "logical addressing" in the documentation.

Your example use a given constant as the value to insert; if that value
is v(1) then simply use it as the value in the logic test. The test as
I wrote it looks at it two ways -- first the location of the 1's is

(v==1)

and to find the location following it that equals the target looks at
the difference in values compensated by the "1" so is one under the
actual magnitude wanted (the ending '0' is just padding to make the
differenced vector the same length as the original so the logical 'and'
works over the same number of elements).

[diff(v)~=5 0]

For the case where the insertion value is the first value in the vector
that can simply be rewritten as

[diff(v)~=(v(1)-1) 0]

I used that logic because it wasn't clear from the description that the
first position in the vector might not be a '1', too. But, if the first
position is always the insertion value and never one, it can be simpler

ix=(v==1)&[(v(2:end)~=v(1) 0];

It still needs the padding to match lengths, however.

--

Subject: Inserting/Pushing a New Value into a Vector

From: james bejon

Date: 3 Sep, 2013 21:33:09

Message: 6 of 19

"Tiger" wrote in message <l04v83$gh3$1@newscl01ah.mathworks.com>...
> Hi dpb,
>
> Thanks for your quick response. Is this a dynamic way of doing it i.e. given any size of a vector, I would like to locate the "1s" and see if the element I want to insert already exists there before actually assigning it.
>
> For instance, if I have a vector =
> 6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1
>
> Basically, I'd like to see where all the "1s" occur and whether the first value of the vector i.e. "6" falls right after each one of them, if not, then I want to add it.
>
> The output I'm trying to achieve is:
> 6 2 1 6 4 1 6 5 2 1 6 5 3 1 6 5 4 1... and there are other vector sizes I'd like to manipulate, however the principle of searching for the "1s" and ainserting a particular value remains the same throughout the various vector sizes.
>
> How's that possible. Many thanks indeed, ta.

% Not the quickest or the cleanest, but...

n = [6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1];
regexprep(char(n+'@'), 'A([^F])', 'AF$1')-'@'

Subject: Inserting/Pushing a New Value into a Vector

From: Roger Stafford

Date: 3 Sep, 2013 22:57:10

Message: 7 of 19

"Tiger" wrote in message <l04q8p$ivg$1@newscl01ah.mathworks.com>...
> Here's a sample vector, Vec = [6, 2 , 1 , 6 , 5 , 2 , 1 , 5 , 3 , 1 , 5 , 4 , 1 , 6 , 4 , 1];
> I'd like to able to add, NewVal = 6, straight after the occurance of element "1", and if "6" already exists after it, then do nothing.
- - - - - - - - -
  In your example, you had a trailing '1' but didn't follow it with a '6' (NewVal). I am not sure whether that was intentional or inadvertent. The following two code snippets do it either way.

//////////////////////////
% Don't follow a trailing '1' with '6'
NewVal = 6;
f = find([false,(Vec(1:end-1)==1&Vec(2:end)~=NewVal)]);
p = cumsum(accumarray(f,1,length(Vec))+1);
v = repmat(NewVal,1,p(end));
v(p) = Vec;

The result is in vector 'v'.
//////////////////////////
% Do follow a trailing '1' with '6'
NewVal = 6;
f = find([false,(Vec==1&[Vec(2:end)~=NewVal,true])]);
p = cumsum(accumarray(f,1,length(Vec)+1)+1);
v = repmat(NewVal,1,p(end)-1);
v(p(1:end-1)) = Vec;

Again the result is in vector 'v'.
//////////////////////////

Roger Stafford

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 6 Sep, 2013 15:13:05

Message: 8 of 19

"james bejon" wrote in message <l05kil$mn9$1@newscl01ah.mathworks.com>...
> "Tiger" wrote in message <l04v83$gh3$1@newscl01ah.mathworks.com>...
> > Hi dpb,
> >
> > Thanks for your quick response. Is this a dynamic way of doing it i.e. given any size of a vector, I would like to locate the "1s" and see if the element I want to insert already exists there before actually assigning it.
> >
> > For instance, if I have a vector =
> > 6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1
> >
> > Basically, I'd like to see where all the "1s" occur and whether the first value of the vector i.e. "6" falls right after each one of them, if not, then I want to add it.
> >
> > The output I'm trying to achieve is:
> > 6 2 1 6 4 1 6 5 2 1 6 5 3 1 6 5 4 1... and there are other vector sizes I'd like to manipulate, however the principle of searching for the "1s" and ainserting a particular value remains the same throughout the various vector sizes.
> >
> > How's that possible. Many thanks indeed, ta.
>
> % Not the quickest or the cleanest, but...
>
> n = [6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1];
> regexprep(char(n+'@'), 'A([^F])', 'AF$1')-'@'

Hi James,
Your method is simply awesome. Many thanks for your effort, and it is neat though. Is there any way I can make the 'new value' to be always the first element of the vector? Ta

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 6 Sep, 2013 17:08:05

Message: 9 of 19

dpb <none@non.net> wrote in message <l05749$hp4$1@speranza.aioe.org>...
> On 9/3/2013 10:29 AM, Tiger wrote:
>
> ...[Please don't toppost--hard conversation follow makes]...
>
> > Hi dpb,
> >
> > Thanks for your quick response. Is this a dynamic way of doing it i.e.
> > given any size of a vector, I would like to locate the "1s" and see if
> > the element I want to insert already exists there before actually
> > assigning it.
> >
> > For instance, if I have a vector =
> > 6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1
> >
> > Basically, I'd like to see where all the "1s" occur and whether the
> > first value of the vector i.e. "6" falls right after each one of them,
> > if not, then I want to add it.
> >
> ...
>
> Sure, read up on "logical addressing" in the documentation.
>
> Your example use a given constant as the value to insert; if that value
> is v(1) then simply use it as the value in the logic test. The test as
> I wrote it looks at it two ways -- first the location of the 1's is
>
> (v==1)
>
> and to find the location following it that equals the target looks at
> the difference in values compensated by the "1" so is one under the
> actual magnitude wanted (the ending '0' is just padding to make the
> differenced vector the same length as the original so the logical 'and'
> works over the same number of elements).
>
> [diff(v)~=5 0]
>
> For the case where the insertion value is the first value in the vector
> that can simply be rewritten as
>
> [diff(v)~=(v(1)-1) 0]
>
> I used that logic because it wasn't clear from the description that the
> first position in the vector might not be a '1', too. But, if the first
> position is always the insertion value and never one, it can be simpler
>
> ix=(v==1)&[(v(2:end)~=v(1) 0];
>
> It still needs the padding to match lengths, however.
>
> --

Thanks dpb,
I managed to get it working now.

ix=(V==1)&[(V(2:end))~=V(1) 0];
V=num2cell(V);
V(ix)={[1 cell2mat((V(1)))]};
V = cell2mat(V)

Cheers.

Subject: Inserting/Pushing a New Value into a Vector

From: james bejon

Date: 6 Sep, 2013 19:16:06

Message: 10 of 19

"Tiger" wrote in message <l0cre1$c04$1@newscl01ah.mathworks.com>...
> "james bejon" wrote in message <l05kil$mn9$1@newscl01ah.mathworks.com>...
> > "Tiger" wrote in message <l04v83$gh3$1@newscl01ah.mathworks.com>...
> > > Hi dpb,
> > >
> > > Thanks for your quick response. Is this a dynamic way of doing it i.e. given any size of a vector, I would like to locate the "1s" and see if the element I want to insert already exists there before actually assigning it.
> > >
> > > For instance, if I have a vector =
> > > 6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1
> > >
> > > Basically, I'd like to see where all the "1s" occur and whether the first value of the vector i.e. "6" falls right after each one of them, if not, then I want to add it.
> > >
> > > The output I'm trying to achieve is:
> > > 6 2 1 6 4 1 6 5 2 1 6 5 3 1 6 5 4 1... and there are other vector sizes I'd like to manipulate, however the principle of searching for the "1s" and ainserting a particular value remains the same throughout the various vector sizes.
> > >
> > > How's that possible. Many thanks indeed, ta.
> >
> > % Not the quickest or the cleanest, but...
> >
> > n = [6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1];
> > regexprep(char(n+'@'), 'A([^F])', 'AF$1')-'@'
>
> Hi James,
> Your method is simply awesome. Many thanks for your effort, and it is neat though. Is there any way I can make the 'new value' to be always the first element of the vector? Ta

I'm not sure I know exactly what you mean. Could you give me an example of the result you're after?

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 7 Sep, 2013 18:54:06

Message: 11 of 19

"james bejon" wrote in message <l0d9lm$inb$1@newscl01ah.mathworks.com>...
> "Tiger" wrote in message <l0cre1$c04$1@newscl01ah.mathworks.com>...
> > "james bejon" wrote in message <l05kil$mn9$1@newscl01ah.mathworks.com>...
> > > "Tiger" wrote in message <l04v83$gh3$1@newscl01ah.mathworks.com>...
> > > > Hi dpb,
> > > >
> > > > Thanks for your quick response. Is this a dynamic way of doing it i.e. given any size of a vector, I would like to locate the "1s" and see if the element I want to insert already exists there before actually assigning it.
> > > >
> > > > For instance, if I have a vector =
> > > > 6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1
> > > >
> > > > Basically, I'd like to see where all the "1s" occur and whether the first value of the vector i.e. "6" falls right after each one of them, if not, then I want to add it.
> > > >
> > > > The output I'm trying to achieve is:
> > > > 6 2 1 6 4 1 6 5 2 1 6 5 3 1 6 5 4 1... and there are other vector sizes I'd like to manipulate, however the principle of searching for the "1s" and ainserting a particular value remains the same throughout the various vector sizes.
> > > >
> > > > How's that possible. Many thanks indeed, ta.
> > >
> > > % Not the quickest or the cleanest, but...
> > >
> > > n = [6 2 1 6 4 1 6 5 2 1 5 3 1 5 4 1];
> > > regexprep(char(n+'@'), 'A([^F])', 'AF$1')-'@'
> >
> > Hi James,
> > Your method is simply awesome. Many thanks for your effort, and it is neat though. Is there any way I can make the 'new value' to be always the first element of the vector? Ta
>
> I'm not sure I know exactly what you mean. Could you give me an example of the result you're after?

Hi James, thanks for your response. I still see your equation to be most simple to implement in my algorithm. Basically, I'm dealing with a large number of rows. I have two important notions; jobs and tasks. A job may contain a number of tasks inside it. So far your equation can handle one job (with different tasks inside it), but when I want to put your equation into a foor loop to run for different jobs, I fail to have it worked successfully.

For instance, let's say I have the following text file:
jID tID Deps
1 1 none
1 2 1
1 3 1
1 4 1
1 5 2,3,4
2 6 none
2 7 6
2 8 6
3 9 none
3 10 9
3 11 9
3 12 9,10,11

You see, each job can have multiple task-paths. the coloumns are Job-ID, Task-ID, Dependencies. Assume I can read from that text file with no issues at all, I know you might think having 'none' might me problematic, but I solved that issue anyway. Now, I made an algorithm that scans the dependencies of tasks and when it reaches to the last Task of a job it produces a vector that has all the possible paths that job has. However, I'm failing at inserting the Task-ID in the last vector. I know this is confusing already but please bare with me.

For instance, if we take the first job, using my algorithm when I reach the 5th, and last, task I get a vector like this = [5 2 1 3 1 4 1], where I'm expecting something like this = [5 2 1 5 3 1 5 4 1] so I can then easily dissect that vector into all the three posisble paths that job has which are 5-2-1, 5-3-1, and 5-4-1.

So far, your equation works well, but if I put it inside a loop to run for the different jobs inside the same text file, it fails because there are two variables I need to update per job which are the LastTaskID (e.g. 5), and the FirstTaskID (e.g. 1). Basically, I want to scan the vector (of the last task of every job) and look for the FirstTaskID whether it has LastTaskID after it or not, if yes then cool otherwise do insert LastTaskID after FirstTaskID but not the after the last one in the vector. Your equation does this quite well, but only for one job at a time.

I hope I've delivered my problem clearly here.
Cheers :-)

Subject: Inserting/Pushing a New Value into a Vector

From: james bejon

Date: 8 Sep, 2013 11:05:07

Message: 12 of 19

Hmmm...my method may not be the best for you then as it gets a bit messy, not that it was ever that clean. You could try the following:

n = [5 2 1 3 1 4 1];
regexprep(char(n+'@'), sprintf('A([^%s])', n(1)+'@'), sprintf('A%s$1', n(1)+'@'))-'@'

If that's not right, you can always pass in the args, e.g.

f = @(n, lo, hi) regexprep(char(n+'@'), sprintf('%s([^%s])', lo+'@', hi+'@'), sprintf('%s%s$1', lo+'@', hi+'@'))-'@';
f(n, 1, 5)

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 9 Sep, 2013 06:16:09

Message: 13 of 19

"james bejon" wrote in message <l0hll3$o0e$1@newscl01ah.mathworks.com>...
> Hmmm...my method may not be the best for you then as it gets a bit messy, not that it was ever that clean. You could try the following:
>
> n = [5 2 1 3 1 4 1];
> regexprep(char(n+'@'), sprintf('A([^%s])', n(1)+'@'), sprintf('A%s$1', n(1)+'@'))-'@'
>
> If that's not right, you can always pass in the args, e.g.
>
> f = @(n, lo, hi) regexprep(char(n+'@'), sprintf('%s([^%s])', lo+'@', hi+'@'), sprintf('%s%s$1', lo+'@', hi+'@'))-'@';
> f(n, 1, 5)

Hi James,

I tried putting your equation in a for-loop to handle different vectors (of different sizes), and I got the following error:
??? Error using ==> plus
Integers can only be combined with integers of the same class, or scalar doubles.

Error in ==>
@(PathOld,lo,hi)regexprep(char(PathOld+'@'),sprintf('%s([^%s])',lo+'@',hi+'@'),sprintf('%s%s$1',lo+'@',hi+'@'))-'@'


Error in ==> tD_vi at 85
              f(PathOld, Low, High);

Mind you, the last line of your code is the one containing the two variables I need to update per vector. I made two varibales, Low=min(n); High=max(n); why did I get the error?

In fact, that's my version of your code that I'm using in my algorithm:
              PathOld = strtok(jPath(i,:),0); % jPath(i,:) changes sizez each iteration. Can that be the source of my problem here?
              f = @(PathOld, lo, hi)regexprep(char(PathOld+'@'), sprintf('%s([^%s])', lo+'@', hi+'@'), sprintf('%s%s$1', lo+'@', hi+'@'))-'@';
              Low = min(PathOld); High = max(PathOld);
              f(PathOld, Low, High);

Any tips? Cheers, man.

Subject: Inserting/Pushing a New Value into a Vector

From: james bejon

Date: 10 Sep, 2013 10:47:04

Message: 14 of 19

Not sure. Try coercing Low and High into doubles, i.e., double(Low) and double(High)

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 18 Sep, 2013 14:23:07

Message: 15 of 19

"james bejon" wrote in message <l0mtb8$qg6$1@newscl01ah.mathworks.com>...
> Not sure. Try coercing Low and High into doubles, i.e., double(Low) and double(High)

Hi James,

I have a slightly different predicament here, if I may ask.
Assume the following:
jPath = [9,4,1,2,5,2,4,1,5,2,3,5,0,0,0,0,0];
tDeps = [4, 5];
tID = 9;

I need to find the locations of tDeps in jPath then insert tID before their values, in other words I need an output like the following:
jPath = [9 4 1 2 9 5 2 9 4 1 9 5 2 3 9 5 0 0 0 0 0]

All of the above variables do change every loop, I can handle the loops but need an equation algorithm that does the insertion right first.

Cheers pal :-)

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 18 Sep, 2013 14:56:06

Message: 16 of 19

"Tiger" wrote in message <l1cd0b$f9k$1@newscl01ah.mathworks.com>...
> "james bejon" wrote in message <l0mtb8$qg6$1@newscl01ah.mathworks.com>...
> > Not sure. Try coercing Low and High into doubles, i.e., double(Low) and double(High)
>
> Hi James,
>
> I have a slightly different predicament here, if I may ask.
> Assume the following:
> jPath = [9,4,1,2,5,2,4,1,5,2,3,5,0,0,0,0,0];
> tDeps = [4, 5];
> tID = 9;
>
> I need to find the locations of tDeps in jPath then insert tID before their values, in other words I need an output like the following:
> jPath = [9 4 1 2 9 5 2 9 4 1 9 5 2 3 9 5 0 0 0 0 0]
>
> All of the above variables do change every loop, I can handle the loops but need an equation algorithm that does the insertion right first.
>

I worked on your equation with my new problem, here's my version of your code:

jPath = [9,4,1,2,5,2,4,3];
tDeps = [4, 5];
tID = 9;
for i = 1:length(tDeps)
    tDepsNow = tDeps(i);
    f = @(jPath, tDepsNow, tID)regexprep(char(jPath+'@'), sprintf('%s([^%s])', tDepsNow+'@', tID+'@'), sprintf('%s%s$1', tDepsNow+'@', tID+'@'))-'@';
    jPath = f(jPath, tDepsNow, tID)
end

The output is almost what I want, but the issue it places 9 after the specified location, I know there must be something little I need to fix, but can't seem to figure it out.

The output I'm expecting is: 9, 4, 1, 2, 9, 5, 2, 9, 4, 3
The output I'm getting from the above code is: 9, 4, 9, 1, 2, 5, 9, 2, 4, 9, 3

Any suggestions?
> Cheers pal :-)

Subject: Inserting/Pushing a New Value into a Vector

From: james bejon

Date: 18 Sep, 2013 21:30:05

Message: 17 of 19

% Data
in = [9 4 1 2 5 2 4 3];
desiredOut = [9 4 1 2 9 5 2 9 4 3];
toks = [4 5];
precedeWith = 9;


% Engine
patt = sprintf('([^%s])([%s])', precedeWith +'@', toks+'@');
repl = sprintf('$1%s$2', precedeWith +'@');
out = regexprep( char('@'+in), patt, repl)-'@';


% Check
isequal( out, desiredOut )

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 19 Sep, 2013 10:14:08

Message: 18 of 19

"james bejon" wrote in message <l1d60t$1sl$1@newscl01ah.mathworks.com>...
> % Data
> in = [9 4 1 2 5 2 4 3];
> desiredOut = [9 4 1 2 9 5 2 9 4 3];
> toks = [4 5];
> precedeWith = 9;
>
>
> % Engine
> patt = sprintf('([^%s])([%s])', precedeWith +'@', toks+'@');
> repl = sprintf('$1%s$2', precedeWith +'@');
> out = regexprep( char('@'+in), patt, repl)-'@';
>
>
> % Check
> isequal( out, desiredOut )

Cheers James, awesome.

Subject: Inserting/Pushing a New Value into a Vector

From: Tiger

Date: 19 Sep, 2013 14:54:07

Message: 19 of 19

"Tiger" wrote in message <l1eipg$n9v$1@newscl01ah.mathworks.com>...
> "james bejon" wrote in message <l1d60t$1sl$1@newscl01ah.mathworks.com>...
> > % Data
> > in = [9 4 1 2 5 2 4 3];
> > desiredOut = [9 4 1 2 9 5 2 9 4 3];
> > toks = [4 5];
> > precedeWith = 9;
> >
> >
> > % Engine
> > patt = sprintf('([^%s])([%s])', precedeWith +'@', toks+'@');
> > repl = sprintf('$1%s$2', precedeWith +'@');
> > out = regexprep( char('@'+in), patt, repl)-'@';
> >
> >
> > % Check
> > isequal( out, desiredOut )
>
> Cheers James, awesome.
Hi again James,

I've been dealing with an interesting predicament here.
Let's assume the first case:
in = [6 4 1 4 2];
toks = [1 4];
precedeWith = 6;
desiredOut = [6 4 1 6 4 2];

Let's assume the second case:
in = [9 4 1 4 2 5 2];
toks = [4 5];
precedeWith = 6;
desiredOut = [9 4 1 9 4 2 9 5 2];

I managed to solve each one of them with slight variations of your algorithm (Engine), but I'm failing at solving both in one go i.e. inside a loop.

Here's my attempt so far:
SubDeps = [4 5];
PathOld = [9,4,1,4,2,9,5,2];
for ii = 1:length(SubDeps)
    tDepsNow = SubDeps(ii);
    tIDnow = 9;
    patt = sprintf('([^%s])([%s])', tIDnow +'@', tDepsNow+'@');
    repl = sprintf('$1%s$2', tIDnow +'@');
    PathNew = regexprep( char('@'+PathOld), patt, repl)-'@'
end

I think my problem lies in not being able to pass the output from the first token as the new "in" for the second token, right? If so, how can I overcome this? Ta

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