I'm trying to equate each element to an array which correspond to cell element. To explain it more precisely, e.g
A = {[1 1 1], [0 0 0 0 0], [1 1],[0 0 0 0 0]};
B = [0 1 0 0];
So the thing I want is :
A= {[0 0 0],[1 1 1 1 1],[0 0],[0 0 0 0 0]};
Possible solution with for loop is :
for ii=1:length(A)
A{ii}(:)=B(ii);
end
Is there any methode which does not use loop?
Using repelem and mat2cell
lens = cellfun(@numel, A);
out = mat2cell(repelem(B,lens).*ones(1,sum(lens)),1,lens)
Note:
cellfun is looping in disguise. But, here cellfun is used to
find the number of elements alone. So this could be considered
almost vectorised :Prepelem function is introduced in R2015a. You may not be able to
run this in prior versions. Instead you may create your own custom repelem function. Refer this answerIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With