How to repeat rows of a matrix n times when n varies with each row?
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Andrei Bobrov
on 21 Nov 2013
Edited: Andrei Bobrov
on 21 Nov 2013
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),:);
0 Comments
More Answers (1)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!