I have a 2-column matrix that describes a piece of data, and the number of times that data occurred within a set:
A = [1 6
2 2
3 8
4 1
5 3];
Given that, is there an "elegant" way to produce the underlying dataset? i.e.,
B = [1 1 1 1 1 1 2 2 3 3 3 3 3 3 3 3 4 5 5 5];
There are plenty of ways to go from B
to A
(tabulate
, using unique
and histc
, etc) but I couldn't find any way to go from A
to B
. The best I could do is not elegant:
B = [];
for ii = 1:size(A,1)
B = [B repmat(A(ii,1), 1, A(ii,2))];
end
I have a sneaking suspicion the "correct" way to do this is to use bsxfun
or accumarray
, but I am not experienced enough to understand how these really work.
You can use 'arrayfun' in combination with 'cell2mat':
This results in