From: Jonathan on
what is the best way to 'invert' an histogram, i.e.,


x = [1, 1, 2, 2, 2, 2, 3]

n = hist(x, 1:3) => n = [2, 4, 1]

how can I generate 'x' from 'n' (without loops of course) ?

thanks in advance,
Jonathan
From: Oleg Komarov on
Jonathan <jonathan.rubin(a)gmail.com> wrote in message <8bfd8f88-70f4-4ae8-8988-7e4a218b8140(a)x22g2000yqx.googlegroups.com>...
> what is the best way to 'invert' an histogram, i.e.,
>
>
> x = [1, 1, 2, 2, 2, 2, 3]
>
> n = hist(x, 1:3) => n = [2, 4, 1]
>
> how can I generate 'x' from 'n' (without loops of course) ?
>
> thanks in advance,
> Jonathan

vakues = 1:3;
% Fastest
invH1 = cell2mat(arrayfun(@(x,y) repmat(y,1,x), n,values,'un',0));
% Another alternative
invH2 = cell2mat(arrayfun(@(x,y) kron(y,ones(1,x)), n,values,'un',0));
% Slowest
invH3 = zeros(1, sum(n));
pos = cumsum([0 n]);
for ii = 1:numel(values)
invH3(pos(ii)+1:pos(ii+1)) = repmat(values(ii),1,n(ii));
end

Oleg
From: Jos (10584) on
Jonathan <jonathan.rubin(a)gmail.com> wrote in message <8bfd8f88-70f4-4ae8-8988-7e4a218b8140(a)x22g2000yqx.googlegroups.com>...
> what is the best way to 'invert' an histogram, i.e.,
>
>
> x = [1, 1, 2, 2, 2, 2, 3]
>
> n = hist(x, 1:3) => n = [2, 4, 1]
>
> how can I generate 'x' from 'n' (without loops of course) ?
>
> thanks in advance,
> Jonathan

This process is called run-length decoding. Search the FEX for some excellent tools on this, and especially look at:

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

n = [2 4 1] ;
rude([2 4 1],1:numel(n))
% -> [1 1 2 2 2 2 3]

hth
Jos
From: ImageAnalyst on
Jonathan:
You can get the values of x back (using methods like the others showed
you), but you can't get their positions back. This is of course
because
x1=[1, 1, 2, 2, 2, 2, 3]
and
x2=[3, 1, 2, 2, 1, 2, 2]
both have the same histogram (what you're non-descriptively calling
"n"). They have the same elements, just in different positions, and
"n" will be identical.

From: Jos (10584) on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <dcdc97c3-5d01-4575-a42c-8d2bf8cbb57e(a)y7g2000vbb.googlegroups.com>...
> Jonathan:
> You can get the values of x back (using methods like the others showed
> you), but you can't get their positions back. This is of course
> because
> x1=[1, 1, 2, 2, 2, 2, 3]
> and
> x2=[3, 1, 2, 2, 1, 2, 2]
> both have the same histogram (what you're non-descriptively calling
> "n"). They have the same elements, just in different positions, and
> "n" will be identical.

Right! but thist might be of interest:

x = [3 1 2 2 1 2 2]
e = [1 2 3]
[n, ix] = histc(x,e)
x2 = e(ix)

hth
Jos