[Research] Create Hierarchical Tree in Matlab using Struct

It really took me quite a while to figure this out. Just take notes for myself.

Syntax 1 Create an array of struct

tree(1,10) = struct(‘a’, []);

image

Syntax 2 Create an array of struct, each field of which is assigned a struct of itself

[tree(1:10).b] = deal(tree);

image (1)

The function: k is the number of brunches, L is the number of levels. inverted_file is the  matrix data need to be assigned to each node in the tree structure.

function tree = create_tree(k, L)

    %leaves
    subtree(1,k) = struct('inverted_file', []);

    %grow to the roots
    for i = 1:L-1
        [subtree(1:k).subtree]=deal(subtree);
    end

    %wrap it in a tree structure
    tree = struct('depth', L, 'branch', k, 'subtree', subtree);

end

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: