Thread Subject: generating a indexing vector of "tied rank"

Subject: generating a indexing vector of "tied rank"

From: Siyi Deng

Date: 19 Mar, 2010 21:09:04

Message: 1 of 3

Hi all, I'm stuck with this problem of making a tied-rank indexing vector:
suppose we have a logical vector

x = rand(1,9) > 0.6; x(1) = 1; x(end) = 1;
% x = [1 1 0 1 0 0 0 1 1];

I want to generate a vector y for the indices of 1s, while 0s are tied to the place of first 1s before them. for example:

% y = [1 2 2 4 4 4 4 8 9];

My current solution is:

x1 = 1:numel(x);
t0 = x1(x);
[~,t1] = histc(x1,t0)
y = t0(t1)

I'm pretty sure this has been discussed before, I think there should be simpler solutions using cumsum, diff, etc . I need to do this on large scale and speed is important. Thanks!

Subject: generating a indexing vector of "tied rank"

From: Oleg Komarov

Date: 19 Mar, 2010 22:35:04

Message: 2 of 3

"Siyi Deng" <mr.siyi.deng@gmail.com> wrote in message <ho0p5g$c85$1@fred.mathworks.com>...
> Hi all, I'm stuck with this problem of making a tied-rank indexing vector:
> suppose we have a logical vector
>
> x = rand(1,9) > 0.6; x(1) = 1; x(end) = 1;
> % x = [1 1 0 1 0 0 0 1 1];
>
> I want to generate a vector y for the indices of 1s, while 0s are tied to the place of first 1s before them. for example:
>
> % y = [1 2 2 4 4 4 4 8 9];
>
> My current solution is:
>
> x1 = 1:numel(x);
> t0 = x1(x);
> [~,t1] = histc(x1,t0)
> y = t0(t1)
>
> I'm pretty sure this has been discussed before, I think there should be simpler solutions using cumsum, diff, etc . I need to do this on large scale and speed is important. Thanks!


y = find(x);
y = y(cumsum(x));

Oleg

Subject: generating a indexing vector of "tied rank"

From: Siyi Deng

Date: 19 Mar, 2010 23:17:03

Message: 3 of 3

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ho0u6o$2g0$1@fred.mathworks.com>...
> "Siyi Deng" <mr.siyi.deng@gmail.com> wrote in message <ho0p5g$c85$1@fred.mathworks.com>...
> > Hi all, I'm stuck with this problem of making a tied-rank indexing vector:
> > suppose we have a logical vector
> >
> > x = rand(1,9) > 0.6; x(1) = 1; x(end) = 1;
> > % x = [1 1 0 1 0 0 0 1 1];
> >
> > I want to generate a vector y for the indices of 1s, while 0s are tied to the place of first 1s before them. for example:
> >
> > % y = [1 2 2 4 4 4 4 8 9];
> >
> > My current solution is:
> >
> > x1 = 1:numel(x);
> > t0 = x1(x);
> > [~,t1] = histc(x1,t0)
> > y = t0(t1)
> >
> > I'm pretty sure this has been discussed before, I think there should be simpler solutions using cumsum, diff, etc . I need to do this on large scale and speed is important. Thanks!
>
>
> y = find(x);
> y = y(cumsum(x));
>
> Oleg

This is indeed a much nicer solution! thanks.

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