0

In Matlab, I need help in preallocating nested cell array and initializing it to zeros. Problem description: I have a numeric cell array, for example, called bn. This array should be preallocatted in following way:

bn{1,1} = 0
bn{1,2}{1,1} = 0
bn{1,2}{1,2}{1,1} = 0
bn{1,2}{1,2}{1,2}{1,2} = 0

enter image description here

I also tried to describe my question with help of an image, assuming I have only three levels. Actually, I have around thirty.

Probably, with for-loop this problem can be solved . But I don't have enough imagination :-( So, please, help me experts!

Wolfie
  • 27,562
  • 7
  • 28
  • 55
ofey
  • 1
  • 4
  • 5
    I cannot express how much of a bad idea this is. How would you even get the data back out? What could possibly be the use of such a construct? I ask because there must be a better solution to your problem than implementing this monstrosity. Also, a “numeric cell array” doesn’t exist. There are cell arrays, and numeric arrays. – Cris Luengo Feb 23 '21 at 15:35
  • I work with metalanguage syntax description of simulation software using Backus-Naur Notation. This tool is in Matlab implemented and for that I need stack, where are different indexes are saved. – ofey Feb 23 '21 at 15:45
  • The stack idea I have taken from here: https://de.mathworks.com/matlabcentral/fileexchange/25162-stack The problem ist that this stack is very time consuming (I call this functions around 1 000 000 times).The plan is create from beginning this stack and fill it with zeros oder [] not changing size of the stack during running the tool. Do you have another idea how I can improve it? – ofey Feb 23 '21 at 15:52
  • 2
    Just preallocate a numeric array. The assumption of the linked address is now incorrect and dynamically adding to the end of array currently is efficient. For example see [this](https://stackoverflow.com/a/48353598/6579744). – rahnema1 Feb 23 '21 at 16:07
  • Stack logic: push is `stack(end+1)=value`, read top value is `stack(end)`, pop is `stack(end)=[]`. Any class or complexity you build around that is going to slow you down, most likely. That said, you can also [try using a Java stack](https://stackoverflow.com/questions/4163920/matlab-stack-data-structure), always time things and don’t make assumptions about speed. – Cris Luengo Feb 23 '21 at 18:14

1 Answers1

1

There are some valid points raised in the comments about this likely not being the most efficient way to store your data. However, assuming there is a specific reason, you could generate this with a simple recursive function:

bn = createNode( 1, 3 );

function node = createNode( currentLayer, maxLayer )
    if currentLayer == maxLayer
        node = {0,0}; % Bottom layer is just {0,0}
    else
        % Higher layers are {0, {sub-node}}
        node = {0, createNode( currentLayer+1, maxLayer )};
    end
end
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • Hi Wolfie, it is exactly the same answer I was looking for. Thanks a lot. Unluckily for me, this solution does not improve the perfomance of my Backus Naur-based tool so much. I will also try to follow alternative way of building the stack, proposed by @Cris Luengo. – ofey Feb 24 '21 at 09:50
  • Using a thirty level nested cell array is never going to be fast. How is this being used/accessed downstream in your code? Recursive calls? Can you show us a code snippet of how you intend to do this? Are the 0 spots always scalars or can they become vectors or matrices? If they stay scalars, then it seems you could simply use a 30x30 matrix for this and store the values in either the lower or upper triangle part, and an equivalent of recursively accessing the nested cell array would simply be index calculations into the triangular matrix. – James Tursa Feb 24 '21 at 17:50
  • Basically, I have 2 functions: pop and push. – ofey Feb 25 '21 at 11:35
  • (1) `function [value1 , value2, bn] = pop(bn)` (2) `function bn = push(value1, value2, bn)`. If I want to pop, then `bn = push(value1, value,2, bn)`, if I want to push, then `bn = push(value1, value2, bn)`. I have 9 scalar variables, in total – ofey Feb 25 '21 at 11:44