How To Build Decision Tree in MATLAB? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report MATLAB is a numerical and programming computation platform that is primarily used for research, modeling, simulation, and analysis in academics, engineering, physics, finance, and biology. MATLAB, which stands for "MATrix LABoratory," was first trying out typical tasks such as matrices operations, linear algebra, and signal processing It has additional use in artificial intelligence, deep learning, and machine learning, and it contains a variety of toolboxes for certain applications including control systems, optimization, and image processing. How to build a decision tree in MATLAB?For this demonstration, we make use of the MATLAB dataset fisheriris which is pre-defined.The fisheriris dataset comprises measurements of iris flowers.PetalLength and PetalWidth are the two parameters we choose to act as predictors, and then we develop a categorical response variable depending on the iris species.Then, use cvpartition function to divide the data into training and testing sets.In order to predict the class labels of the test data, first utilize the training data to build a decision tree using the fitctree function.The classification accuracy is then calculated by contrasting the anticipated labels with the actual labels of the test data.Example 1: Matlab % Loading the Iris dataset. load fisheriris % Splitting the data into training and testing. cv = cvpartition(species,'HoldOut',0.3); Xtrain = meas(cv.training,:); Ytrain = species(cv.training); Xtest = meas(cv.test,:); Ytest = species(cv.test); % Training the decision tree model. tree = fitctree(Xtrain, Ytrain); % Viewing the decision tree. view(tree,'Mode','graph'); % Predicting the classes of the testing set. Ypred = predict(tree, Xtest); % Calculate the accuracy of the model. accuracy = sum(Ypred == Ytest)/length(Ytest); disp(['Accuracy: ' num2str(accuracy)]); Output: Comment More infoAdvertise with us S shivatiwari1003 Follow Improve Article Tags : Software Engineering MATLAB-graphs Explore Software Engineering BasicsIntroduction to Software Engineering7 min readSoftware Development Life Cycle (SDLC)8 min readSoftware Quality - Software Engineering5 min readISO/IEC 9126 in Software Engineering4 min readBoehm's Software Quality Model4 min readSoftware Crisis - Software Engineering3 min readSoftware Measurement & MetricesSoftware Measurement and Metrics4 min readPeople Metrics and Process Metrics in Software Engineering8 min readHalsteadâs Software Metrics - Software Engineering11 min readCyclomatic Complexity6 min readFunctional Point (FP) Analysis - Software Engineering8 min readLines of Code (LOC) in Software Engineering4 min readSoftware Development Models & Agile MethodsWaterfall Model - Software Engineering13 min readWhat is Spiral Model in Software Engineering?9 min readPrototyping Model - Software Engineering7 min readIncremental Process Model - Software Engineering6 min readRapid Application Development Model (RAD) - Software Engineering9 min readCoupling and Cohesion - Software Engineering10 min readAgile Software Development - Software Engineering15+ min readSRS & SPMSoftware Requirement Specification (SRS) Format5 min readSoftware Engineering | Quality Characteristics of a good SRS7 min readSoftware Project Management (SPM) - Software Engineering8 min readCOCOMO Model - Software Engineering15+ min readCapability Maturity Model (CMM) - Software Engineering11 min readIntegrating Risk Management in SDLC | Set 18 min readSoftware Maintenance - Software Engineering14 min readTesting & DebuggingWhat is Software Testing?11 min readTypes of Software Testing15+ min readTesting Guidelines - Software Engineering3 min readWhat is Debugging in Software Engineering?11 min readVerification & ValidationVerification and Validation in Software Engineering6 min readRole of Verification and Validation (V&V) in SDLC5 min readRequirements Validation Techniques - Software Engineering8 min readPractice QuestionsTop 50+ Software Engineering Interview Questions and Answers [2025]15+ min read Like