MATLAB: Assigning row to array using cellfun

cell arraycell arrayscellfunMATLAB

Hello,
I have a 1×4 cell array, each cell contains a 1000×8 zeros array:
A = arrayfun(@(~) zeros(1000,8),(1:4),'un',0);
Now I want to assign every same row (definded by a background counter) with a new array same length, e.g B = [1 1 1 1 1 1 1 1]
The following loop discribes it quite well…
counter = randi(1000) % any row
for i = 1:4
A{i}(counter,:) = B;
end
I was just wondering if there is a way doing it with cellfun?

Best Answer

Here's your loop, within cellfun().
A = cellfun(@(x)[x(1:counter-1,:);B;x(counter+1:end,:)], A, 'UniformOutput', false);