Search code examples
matlabmultidimensional-arrayindexinginitializationcell-array

Can I with logical indexing initilize a cell array with a multidimentinal arrays of variable length


I have a persistent cell array that in one cell should have a three dimentional array where the number of pages is picked from a lookUp array. I want the cell to be indexed by a logical array and a want the number of pages (3rd dimension) in the multidimensional array to be picked by the same logical array. I am trying to avoid using a for loop as the number of elements are very high.

Is there a way? As the maximum number of pages is 8, I am thinking of giving up and just allocate 8 pages for all arrays.

%elements is a list of indexes
elements = 1:5;
%notInitialized is a logical stating that the cell of a element should be
notInitialized = logical(elements>2);
%initialized in the cell array cValuesSaved
lookUpOfNumberofPages = 4:8;

%persistent cValuesSaved
cValuesSaved = cell(numel(elements), 2); % two cells per elements

%I am not good at matlab, but my guess the code I want should look something like this:
cValuesSaved{notInitialized, 1} = nan(4, 2*3, lookUpOfNumberofPages(notInitialized));

Solution

  • So I solved it, and if you want here is my solution. Not the prettiest way to create a1, a2, a3, but I am in a hurry.

    Thanks for those who had a look!

    array = nan(numel(elements), 3);
    array(:,1) = 4;
    array(:,2) = 2*3;
    array(:,3) = lookUpOfNumberofPages;
    a1 = array(:,1);
    a2 = array(:,2);
    a3 = array(:,3);
    
    test2 = arrayfun(@nan, a1, a2, a3, 'UniformOutput', 0);
    cValuesSaved(notInitialized,1) = test2(notInitialized);