Setting the last value of each matrix in a cell array in Matlab

Viewed 113

I have a cell array where each cell contains a matrix of identical size. How do I efficiently set the last entry of each matrix in the array? I was trying to make use of cellfun but it doesn't look like assignment is possible.

Minimal working example (the most efficient implementation I could come up with):

C = cell(5, 6, 7);
[C{:}] = deal(ones(10, 1));
for i = 1:5
    for j = 1:6
        for k = 1:7
            C{i,j,k}(end) = 0;
        end
    end
end
3 Answers

I think the best way to do this would be to change your original input. Could you live without a cell? These are normally only used when you have heterogeneous data.

Try making each cell a page (where pages are the third dimension) of an array (so now it's now just a vanilla 3D array).

Then you should be able to index directly into the last entry of each page.

Related