How to repeat rows of a matrix n times when n varies with each row?

I have obtained counts corresponding to the number of times each row of a matrix A (of size 14000 x 5) occurs. I obtained these counts using
[u, ia, ic]=unique(A,'rows'); C=[u histc(ic,1:length(u))];
Now I want to match the counts i.e histc(ic,1:length(u)) to each row of A, I tried to do the following
CC=zeros(length(A),6); for i=1:length(C) CC(i,:)=repmat(C(i,:),C(i,6),1); end
It works fine for the first 17 instances where C(i,6)=1, but then as soon as I have more than one i.e. C(i,6)=2 etc. Matlab gives me the following error
Subscripted assignment dimension mismatch. Error in CC(i,:)=repmat(C(i,:),C(i,6),1);
I also realize that with length(CC)>length(C) I will have an issue filling out the matrix.
How can I do this? Many thanks IC

 Accepted Answer

CC=zeros(size(A,1),6);
i2 = cumsum(C(:,6));
i1 = i2 - C(:,6) + 1;
for ii=1:size(C,1)
CC(i1(ii):i2(ii),:)=repmat(C(ii,:),C(ii,6),1);
end
or
n = cumsum(C(:,6));
i1 = n - C(:,6) + 1;
idx = zeros(n(end),1);
idx(i1) = 1;
CC = C(cumsum(idx),:);

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Asked:

on 21 Nov 2013

Answered:

on 21 Nov 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!