MATLAB: What is wrong

MATLABmatricesmatrixmatrix arraymatrix manipulation

why it show error
??? Error using ==> cat CAT arguments dimensions are not consistent.
Error in ==> @(i)cat(3,randperm(m)',[11*ones(N(1,i),1);22*ones(N(2,i),1);33*ones(N(3,i),1);44*ones(N(4,i),1)])
m=22;
AA = [m 7];
N=[11,9,11,10,10,9,9;7,7,7,7,7,7,7;4,5,4,4,4,5,5;1,1,1,1,1,1,1];
B = cell2mat(arrayfun(@(i)cat(3,randperm(m)',[11*ones(N(1,i),1);22*ones(N(2,i),1);33*ones(N(3,i),1);44*ones(N(4,i),1)]),1:size(N,2),'un',0));
x = B(sub2ind(szAA,B(:,:,1),cumsum(ones(AA),2))+prod(AA));
i cant understand what the problem,can someone help me?

Best Answer

Hi Sahab! More variant solving your problem
m=22;
AA = [m 7];
N=[11,9,11,10,10,9,9;7,7,7,7,7,7,7;4,5,4,4,4,5,5;1,1,1,1,1,1,1];
t=sum(N)~=AA(1);
if any(t), I1 = randi(size(N,1)-1); N(I1,:) = N(I1,:)-t; end
x = zeros(AA);
xx = 11*(1:4)';
for j = 1:size(N,2)
x(randperm(AA(1)),j) = ...
cell2mat(arrayfun(@(i)xx(i)*ones(N(i,j),1),1:length(xx),'un',0).');
end