code a christmas tree graph in matlab

Here's a sample code to create a Christmas tree graph in MATLAB:

main.m
% Define tree parameters
levels = 10;
width = 40;

% Initialize matrix
tree = repmat(' ', levels, width);

% Add branches to tree
for i = 1:levels
    start = width/2 - (i-1);
    stop = width/2 + (i-1);
    branches = start:stop;
    tree(i, branches) = '#';
end

% Add trunk to tree
trunk_start = floor(width/2) - 2;
trunk_stop = floor(width/2) + 2;
trunk = levels:levels+3;
tree(trunk, trunk_start:trunk_stop) = '|';

% Display tree
disp(tree)
447 chars
24 lines

You can adjust the levels and width parameters to create trees of different sizes.

I hope this helps you!

related categories

gistlibby LogSnag