Option 1
Create a function (e.g. in a separate file)
function x = assignAtEnd(x, v)
x(end) = v;
end
and do cellfunas follows
B = cellfun(@(x) assignAtEnd(x, 0), C, 'UniformOutput', false);
This will give you an array of cells Bwith all values at the end changed to 0.
, , , for. . 1.000.000 (100, 100, 100),
for-loop (3D): 3.48 sec
for-loop (1D as per @mikkola): 2.67 sec
cellfun: 3.06 sec
2
, , , , .
% creating the cell
cellDim = 100;
matrixDim = 10;
C = cell(cellDim, cellDim, cellDim);
[C{:}] = deal(1:matrixDim);
% converting to a 4D numeric matrix
A = reshape(cell2mat(C), cellDim, matrixDim, cellDim, cellDim);
% assigning the 0s
A(:,end,:,:) = 0;
% converting back to a cell
B = squeeze(num2cell(A, 2));
, .
with numeric conversion: 1.93 sec
1.40 sec , 0.5 sec, 5 !
@LuisMendo ,
with numeric conversion (@LuisMendo): 1.66 sec
, ! .