From: Bruno Luong on
"james bejon" <jamesbejon(a)yahoo.co.uk> wrote in message <housrl$1vl$1(a)fred.mathworks.com>...
> There's some really neat stuff here. Thanks so much. I'm very new to Matlab, but am starting to see how concise its code can be. On a related note, Can anyone think of a neat way of converting, say,
>
> [3, 8, 12]
>
> to
>
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?
>
> Or better still:
>
> [3, 8, 12;
> 3, 7, 12]
>
> to
>
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
> 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?

c=[3, 8, 12;
3, 7, 12]

r = repmat((1:size(c,1)).',[1 size(c,2)]);
A = accumarray([r(:) c(:)+1],1);
A(:,1) = 1;
A = cumsum(A,2);
A(:,end) = [];

disp(A)

% Bruno
From: james bejon on
Hi Bruno. Thanks very much for your suggestion. Works perfectly. I like your idea with the cumsum function. In the end, I've gone for something like (I happen to know that all the rows are of length 25)

c = transpose([3, 11, 23, 24; 3, 10, 23, 24]) + 1;
z = zeros(25, size(c, 2));
n = repmat(1:size(c, 2), size(c, 1), 1);
z(sub2ind(size(z), c, n)) = 1;
z = cumsum(z, 1) + 1;

P.S. I nicked the sub2ind idea from your following RANK thread!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/163003#731927