I have one index vector with nine elements,IDX = [1 2 1 3 4 3 1 2 1]
.(in another post, I asked about how to repeat two separate vectors IDX1 and IDX2, but here I have all the elements in one vector)
The first five elements of this matrix are to be repeated according to elements in howMany1 = [3 2 2 1 2]
, the next four elements are to be repeated according to howMany2 = [2 1 2 2]
. So the result would be: a1 =[1 1 1 2 2 1 1 3 4 4]
and a2 = [3 3 1 2 2 1 1 ]
.
How could I do it by MATLAB such that the result is stored in two separate matrices in a cell?
Here is my code, but I can not find out what the problem is. Thank you all for taking the time.
clear all
close all
clc
IDX = [1 2 1 3 4 3 1 2 1];
howMany1 = [3 2 2 1 2]; % howMany1 and howMany2 determine how many times each index in IDX should be repeated.
howMany2 = [2 1 2 2];
%%
howMany = {howMany1 howMany2};
a = cell(2,1)
m = 1
for k = 1:2
for j=m:length(howMany)+m-1
h = howMany{k};
idx=IDX(j);
a{k,1}= cell2mat(arrayfun(@(x,y) repmat(x,1,y),idx,h(j),'uniform',0))
end
m = m+length(h)
end