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:
replace all values matching position in given vector with something else

Subject: replace all values matching position in given vector with something else

From: Misha Koshelev

Date: 6 May, 2009 00:34:02

Message: 1 of 12

Is there a more succinct/vectorized way of doing:

for i=1:length(values)
   a(find(a == values)) = i;
end

Specifically, there seems to be a fast way to do sort of the _opposite_, that is replace values in a vector by their indices in values:

match(a,values);

but what I want is to replace values in a vector corresponding to indices in values by the actual values in values...

does this make sense?

Thanks
Misha

Subject: replace all values matching position in given vector with something else

From: Darren Rowland

Date: 6 May, 2009 02:17:01

Message: 2 of 12


> does this make sense?

No, it does not. Could you give an example please.

Subject: replace all values matching position in given vector with something else

From: Jos

Date: 6 May, 2009 11:59:02

Message: 3 of 12

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gtqltq$k0i$1@fred.mathworks.com>...
> Is there a more succinct/vectorized way of doing:
>
> for i=1:length(values)
> a(find(a == values)) = i;
> end
>
> Specifically, there seems to be a fast way to do sort of the _opposite_, that is replace values in a vector by their indices in values:
>
> match(a,values);
>
> but what I want is to replace values in a vector corresponding to indices in values by the actual values in values...
>
> does this make sense?
>
> Thanks
> Misha

Does REPLACE do the trick?

A = [10 11 10 12 11 13 14 10 15] ;
values = [11 13 10] ;
B = replace(a,values,1:numel(values))
% B = 3 1 3 12 1 2 14 3 15

REPLACE can be found here:
http://www.mathworks.com/matlabcentral/fileexchange/10063

hth
Jos

Subject: replace all values matching position in given vector with something else

From: us

Date: 6 May, 2009 12:19:01

Message: 4 of 12

"Jos " <#10584@fileexchange.com> wrote in message <gtru26$3q2$1@fred.mathworks.com>...
> "Misha Koshelev" <mk144210@bcm.edu> wrote in message <gtqltq$k0i$1@fred.mathworks.com>...
> > Is there a more succinct/vectorized way of doing:
> >
> > for i=1:length(values)
> > a(find(a == values)) = i;
> > end
> >
> > Specifically, there seems to be a fast way to do sort of the _opposite_, that is replace values in a vector by their indices in values:
> >
> > match(a,values);
> >
> > but what I want is to replace values in a vector corresponding to indices in values by the actual values in values...
> >
> > does this make sense?
> >
> > Thanks
> > Misha
>
> Does REPLACE do the trick?
>
> A = [10 11 10 12 11 13 14 10 15] ;
> values = [11 13 10] ;
> B = replace(a,values,1:numel(values))
> % B = 3 1 3 12 1 2 14 3 15
>
> REPLACE can be found here:
> http://www.mathworks.com/matlabcentral/fileexchange/10063

almost forgot about jos's REPLACE... download and rate(!) this great submission...

another solution (for this simple case) might look like this

     a=[10 11 10 12 11 13 14 10 15];
     v=[11 13 10];
     [ix,ix]=ismember(a,v);
     ix(ix==0)=a(ix==0)
% ix = 3 1 3 12 1 2 14 3 15

us

Subject: replace all values matching position in given vector with something else

From: Misha Koshelev

Date: 6 May, 2009 15:52:02

Message: 5 of 12

"us " <us@neurol.unizh.ch> wrote in message <gtrv7l$m64$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gtru26$3q2$1@fred.mathworks.com>...
> > "Misha Koshelev" <mk144210@bcm.edu> wrote in message <gtqltq$k0i$1@fred.mathworks.com>...
> > > Is there a more succinct/vectorized way of doing:
> > >
> > > for i=1:length(values)
> > > a(find(a == values)) = i;
> > > end
> > >
> > > Specifically, there seems to be a fast way to do sort of the _opposite_, that is replace values in a vector by their indices in values:
> > >
> > > match(a,values);
> > >
> > > but what I want is to replace values in a vector corresponding to indices in values by the actual values in values...
> > >
> > > does this make sense?
> > >
> > > Thanks
> > > Misha
> >
> > Does REPLACE do the trick?
> >
> > A = [10 11 10 12 11 13 14 10 15] ;
> > values = [11 13 10] ;
> > B = replace(a,values,1:numel(values))
> > % B = 3 1 3 12 1 2 14 3 15
> >
> > REPLACE can be found here:
> > http://www.mathworks.com/matlabcentral/fileexchange/10063
>
> almost forgot about jos's REPLACE... download and rate(!) this great submission...
>
> another solution (for this simple case) might look like this
>
> a=[10 11 10 12 11 13 14 10 15];
> v=[11 13 10];
> [ix,ix]=ismember(a,v);
> ix(ix==0)=a(ix==0)
> % ix = 3 1 3 12 1 2 14 3 15
>
> us

Hi Replace definitely does the trick. The above is an interesting solution as well.

Thank you
Misha

Subject: replace all values matching position in given vector with something else

From: Misha Koshelev

Date: 6 May, 2009 17:44:02

Message: 6 of 12

"us " <us@neurol.unizh.ch> wrote in message <gtrv7l$m64$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gtru26$3q2$1@fred.mathworks.com>...
> > "Misha Koshelev" <mk144210@bcm.edu> wrote in message <gtqltq$k0i$1@fred.mathworks.com>...
> > > Is there a more succinct/vectorized way of doing:
> > >
> > > for i=1:length(values)
> > > a(find(a == values)) = i;
> > > end
> > >
> > > Specifically, there seems to be a fast way to do sort of the _opposite_, that is replace values in a vector by their indices in values:
> > >
> > > match(a,values);
> > >
> > > but what I want is to replace values in a vector corresponding to indices in values by the actual values in values...
> > >
> > > does this make sense?
> > >
> > > Thanks
> > > Misha
> >
> > Does REPLACE do the trick?
> >
> > A = [10 11 10 12 11 13 14 10 15] ;
> > values = [11 13 10] ;
> > B = replace(a,values,1:numel(values))
> > % B = 3 1 3 12 1 2 14 3 15
> >
> > REPLACE can be found here:
> > http://www.mathworks.com/matlabcentral/fileexchange/10063
>
> almost forgot about jos's REPLACE... download and rate(!) this great submission...
>
> another solution (for this simple case) might look like this
>
> a=[10 11 10 12 11 13 14 10 15];
> v=[11 13 10];
> [ix,ix]=ismember(a,v);
> ix(ix==0)=a(ix==0)
> % ix = 3 1 3 12 1 2 14 3 15
>
> us

Actually I guess what I want is the opposite of the above...

if a=[3 3 2 2 1 1];

and

v=[10 12 11];

I want the resulting vector b to be

b=[11 11 12 12 10 10];

Misha

Subject: replace all values matching position in given vector with something else

From: Misha Koshelev

Date: 6 May, 2009 17:45:03

Message: 7 of 12

"us " <us@neurol.unizh.ch> wrote in message <gtrv7l$m64$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gtru26$3q2$1@fred.mathworks.com>...
> > "Misha Koshelev" <mk144210@bcm.edu> wrote in message <gtqltq$k0i$1@fred.mathworks.com>...
> > > Is there a more succinct/vectorized way of doing:
> > >
> > > for i=1:length(values)
> > > a(find(a == values)) = i;
> > > end
> > >
> > > Specifically, there seems to be a fast way to do sort of the _opposite_, that is replace values in a vector by their indices in values:
> > >
> > > match(a,values);
> > >
> > > but what I want is to replace values in a vector corresponding to indices in values by the actual values in values...
> > >
> > > does this make sense?
> > >
> > > Thanks
> > > Misha
> >
> > Does REPLACE do the trick?
> >
> > A = [10 11 10 12 11 13 14 10 15] ;
> > values = [11 13 10] ;
> > B = replace(a,values,1:numel(values))
> > % B = 3 1 3 12 1 2 14 3 15
> >
> > REPLACE can be found here:
> > http://www.mathworks.com/matlabcentral/fileexchange/10063
>
> almost forgot about jos's REPLACE... download and rate(!) this great submission...
>
> another solution (for this simple case) might look like this
>
> a=[10 11 10 12 11 13 14 10 15];
> v=[11 13 10];
> [ix,ix]=ismember(a,v);
> ix(ix==0)=a(ix==0)
> % ix = 3 1 3 12 1 2 14 3 15
>
> us

Actually I guess what I want is the opposite of the above...

if a=[3 3 2 2 1 1];

and

v=[10 12 11];

I want the resulting vector b to be

b=[11 11 12 12 10 10];

Misha

Subject: replace all values matching position in given vector with something else

From: us

Date: 6 May, 2009 18:02:02

Message: 8 of 12

"Misha Koshelev"
> Actually I guess what I want is the opposite of the above...
> if a=[3 3 2 2 1 1];
> v=[10 12 11];
> I want the resulting vector b to be
> b=[11 11 12 12 10 10];

well, this is very different from your OP - thinking before posting would have save some CSSMers some time...

     a=[3 3 2 2 1 1];
     v=[10 12 11];
     b=v(a)
% b = 11 11 12 12 10 10

us

Subject: replace all values matching position in given vector with something else

From: Matt Fig

Date: 6 May, 2009 18:11:01

Message: 9 of 12

"us " <us@neurol.unizh.ch> wrote in message <gtsjap$s00$1@fred.mathworks.com>...
> "Misha Koshelev"
> > Actually I guess what I want is the opposite of the above...
> > if a=[3 3 2 2 1 1];
> > v=[10 12 11];
> > I want the resulting vector b to be
> > b=[11 11 12 12 10 10];
>
> well, this is very different from your OP - thinking before posting would have save some CSSMers some time...
>
> a=[3 3 2 2 1 1];
> v=[10 12 11];
> b=v(a)
> % b = 11 11 12 12 10 10
>
> us



Thinking, and a clear example which shows inputs and desired output!

Subject: replace all values matching position in given vector with something else

From: Misha Koshelev

Date: 6 May, 2009 21:43:01

Message: 10 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <gtsjrk$6jn$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gtsjap$s00$1@fred.mathworks.com>...
> > "Misha Koshelev"
> > > Actually I guess what I want is the opposite of the above...
> > > if a=[3 3 2 2 1 1];
> > > v=[10 12 11];
> > > I want the resulting vector b to be
> > > b=[11 11 12 12 10 10];
> >
> > well, this is very different from your OP - thinking before posting would have save some CSSMers some time...
> >
> > a=[3 3 2 2 1 1];
> > v=[10 12 11];
> > b=v(a)
> > % b = 11 11 12 12 10 10
> >
> > us
>
>
>
> Thinking, and a clear example which shows inputs and desired output!

Sorry about that. That is pretty neat. Since I am on this topic maybe the following function I made can somehow be replaced/made neater too:

function r = permsplus(set,full_set,num,num_extra)
% PERMSPLUS Find all permutations of the set of a given length, with
% something extra.
% R = PERMSPLUS(SET,FULL_SET,NUM,EXTRA) Find all permutations of SET of
% length NUM, with NUM_EXTRA terms from SET where repeats are
% allowed. FULL_SET is a copy of the full set that is always passed
% complete for the second stage.
    r = [];
    if num > 0
        for i=1:length(set)
            new_set = set;
            new_set(i) = [];
            new_r = [];
            new_r(:,2:num+num_extra) = permsplus(new_set,full_set,num-1,num_extra);
            new_r(:,1) = set(i);
            r = cat(1,r,new_r);
        end
    elseif num_extra > 0
        for i=1:length(full_set)
            new_r = [];
            new_r(:,2:num_extra) = permsplus(set,full_set,num,num_extra-1);
            new_r(:,1) = full_set(i);
            r = cat(1,r,new_r);
        end
    end

It's the same problem basically I'm trying to find the optimal match between two lists of groupings of individuals. The idea here is that when there are more group #s in the first list than the second, I can't just try all permutations of the group #s, but rather "collapse" some groups into those that are present in the second type vector.

To do this I basically create all permutations for the first n groups that are in common, and then for the rest just "collapse" into existing groups allowing repetition. I know there's a perms function that might be easier/faster (?) to use, but I would somehow have to tag it on on the left-hand side.

Thinking about it I suppose there must be some way to combine the output of my recursive function that makes combinations of groupings (the plus part) and simply tag on perms on the left hand side... I'm guessing maybe repmat is the way to do this? Anyway, I'm pretty new to MATLAB and appreciate the help. The above function works but it gets called a lot too so if there's a faster way to do this would be appreciated.

Thanks
Misha

Subject: replace all values matching position in given vector with something else

From: Misha Koshelev

Date: 7 May, 2009 01:47:01

Message: 11 of 12

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gtt095$3u3$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <gtsjrk$6jn$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <gtsjap$s00$1@fred.mathworks.com>...
> > > "Misha Koshelev"
> > > > Actually I guess what I want is the opposite of the above...
> > > > if a=[3 3 2 2 1 1];
> > > > v=[10 12 11];
> > > > I want the resulting vector b to be
> > > > b=[11 11 12 12 10 10];
> > >
> > > well, this is very different from your OP - thinking before posting would have save some CSSMers some time...
> > >
> > > a=[3 3 2 2 1 1];
> > > v=[10 12 11];
> > > b=v(a)
> > > % b = 11 11 12 12 10 10
> > >
> > > us
> >
> >
> >
> > Thinking, and a clear example which shows inputs and desired output!
>
> Sorry about that. That is pretty neat. Since I am on this topic maybe the following function I made can somehow be replaced/made neater too:
>
> function r = permsplus(set,full_set,num,num_extra)
> % PERMSPLUS Find all permutations of the set of a given length, with
> % something extra.
> % R = PERMSPLUS(SET,FULL_SET,NUM,EXTRA) Find all permutations of SET of
> % length NUM, with NUM_EXTRA terms from SET where repeats are
> % allowed. FULL_SET is a copy of the full set that is always passed
> % complete for the second stage.
> r = [];
> if num > 0
> for i=1:length(set)
> new_set = set;
> new_set(i) = [];
> new_r = [];
> new_r(:,2:num+num_extra) = permsplus(new_set,full_set,num-1,num_extra);
> new_r(:,1) = set(i);
> r = cat(1,r,new_r);
> end
> elseif num_extra > 0
> for i=1:length(full_set)
> new_r = [];
> new_r(:,2:num_extra) = permsplus(set,full_set,num,num_extra-1);
> new_r(:,1) = full_set(i);
> r = cat(1,r,new_r);
> end
> end
>
> It's the same problem basically I'm trying to find the optimal match between two lists of groupings of individuals. The idea here is that when there are more group #s in the first list than the second, I can't just try all permutations of the group #s, but rather "collapse" some groups into those that are present in the second type vector.
>
> To do this I basically create all permutations for the first n groups that are in common, and then for the rest just "collapse" into existing groups allowing repetition. I know there's a perms function that might be easier/faster (?) to use, but I would somehow have to tag it on on the left-hand side.
>
> Thinking about it I suppose there must be some way to combine the output of my recursive function that makes combinations of groupings (the plus part) and simply tag on perms on the left hand side... I'm guessing maybe repmat is the way to do this? Anyway, I'm pretty new to MATLAB and appreciate the help. The above function works but it gets called a lot too so if there's a faster way to do this would be appreciated.
>
> Thanks
> Misha

And actually my function is wrong. Here's the correct one:

function r = permsplus(set,full_set,num,num_extra)
% PERMSPLUS Find all permutations of the set of a given length, with
% something extra.
% R = PERMSPLUS(SET,FULL_SET,NUM,EXTRA) Find all permutations of SET of
% length NUM, with NUM_EXTRA terms from SET where repeats are
% allowed. FULL_SET is a copy of the full set that is always passed
% complete for the second stage.
    r = [];
    if num > 0
        for i=1:length(set)
            new_set = set;
            new_set(i) = [];
            new_r = [];
            new_r(:,2:num+num_extra) = permsplus(new_set,full_set,num-1,num_extra);
            new_r(:,1) = set(i);
            r = cat(1,r,new_r);
        end
    end
    if num_extra > 0
        for i=1:length(full_set)
            new_r = [];
            new_r(:,2:num+num_extra) = permsplus(set,full_set,num,num_extra-1);
            new_r(:,1) = full_set(i);
            r = cat(1,r,new_r);
        end
    end

Subject: replace all values matching position in given vector with something else

From: Matt Fig

Date: 7 May, 2009 02:03:03

Message: 12 of 12

"Misha Koshelev" <mk144210@bcm.edu> wrote in message
> And actually my function is wrong. Here's the correct one:
>
> function r = permsplus(set,full_set,num,num_extra)
> % PERMSPLUS Find all permutations of the set of a given length, with
> % something extra.
> % R = PERMSPLUS(SET,FULL_SET,NUM,EXTRA) Find all permutations of SET of
> % length NUM, with NUM_EXTRA terms from SET where repeats are
> % allowed. FULL_SET is a copy of the full set that is always passed
> % complete for the second stage.
> r = [];
> if num > 0
> for i=1:length(set)
> new_set = set;
> new_set(i) = [];
> new_r = [];
> new_r(:,2:num+num_extra) = permsplus(new_set,full_set,num-1,num_extra);
> new_r(:,1) = set(i);
> r = cat(1,r,new_r);
> end
> end
> if num_extra > 0
> for i=1:length(full_set)
> new_r = [];
> new_r(:,2:num+num_extra) = permsplus(set,full_set,num,num_extra-1);
> new_r(:,1) = full_set(i);
> r = cat(1,r,new_r);
> end
> end


It looks from the files help like you are trying to do something like this:

http://www.mathworks.com/matlabcentral/fileexchange/11462

Maybe your input args are different. How is your function different?

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