Skip to content
ME-705 · MATLAB and R Programming/Quick Revision Short Notes

MATLAB and R Programming (ME-705) - Unit 4 Short Notes

4.1 Advanced Data Handling & Preprocessing

4.1.1 Complex Data Structures

Feature MATLAB R
Tabular Data table: Heterogeneous columns, row names. <br> timetable: Table with row times for time-series. data.frame: Base structure. <br> tibble (tbl_df): Enhanced data.frame (from tibble package).
Categorical Data categorical: Array of categories. Efficient for discrete data. factor: Categorical vector with levels. <br> forcats package for manipulation.
Date/Time datetime, duration, calendarDuration: Precise time arithmetic. POSIXct, POSIXlt: Base date-time classes. <br> lubridate package: Simplified parsing/arithmetic.

[!TIP] Common Pitfall: In R, stringsAsFactors = FALSE (default in recent versions) prevents automatic conversion of strings to factors. In MATLAB, table variables can be accessed via dot notation (T.VarName).

4.1.2 Data Import/Export from Diverse Sources

Source MATLAB Functions R Packages/Functions
Text/Spreadsheet readtable, readmatrix, writetable, writematrix readr: read_csv, read_tsv (fast). <br> data.table: fread (very fast).
Databases Database Toolbox: database, exec, fetch. DBI + backend (e.g., RPostgres, RSQLite): dbConnect, dbReadTable.
Web/API webread, webwrite (JSON/XML). Basic scraping with htmlTree. jsonlite: fromJSON, toJSON. <br> xml2: read_xml. <br> rvest for web scraping.

4.1.3 Data Cleaning & Transformation Pipelines

  • Handling Missing Data:

    • Imputation Strategies: Mean/median/mode filling, forward/backward fill (fillmissing in MATLAB, tidyr::fill in R), model-based imputation.

    • Removal: rmmissing (MATLAB), na.omit (R).

  • Reshaping Data:

    • Wide to Long (Melt): MATLAB: stack. R: tidyr::pivot_longer().

    • Long to Wide (Pivot): MATLAB: unstack. R: tidyr::pivot_wider().

  • Pipeline Operations:

    • MATLAB: Use varfun, rowfun for group-wise operations. Chain with dot notation or findgroups + splitapply.

    • R: dplyr + %>% pipe operator. Key verbs: filter(), select(), mutate(), group_by(), summarise().

[!TIP] Exam Focus: Know the difference between pivot_longer()/pivot_wider() (tidyr, modern) vs gather()/spread() (tidyr, deprecated). In MATLAB, unstack requires a grouping variable.


4.2 Advanced Statistical Modeling & Machine Learning

4.2.1 Supervised Learning

Task Model MATLAB Function(s) R Function(s)/Packages
Regression Multiple Linear fitlm lm()
Polynomial fitlm (with polynomial terms) lm() (with poly() or I(x^2))
Ridge/Lasso fitrlinear ('Learner','ridge'/'lasso') glmnet: glmnet(x, y, alpha=0/1)
Classification Logistic Regression fitglm ('Distribution','binomial') glm(y ~ ., family=binomial)
SVM fitcsvm e1071::svm() or kernlab::ksvm()
Decision Tree fitctree rpart::rpart() or party::ctree()
Random Forest TreeBagger randomForest::randomForest()
k-NN fitcknn class::knn()
Model Evaluation Cross-Validation crossval, cvpartition caret::trainControl(method='cv')
ROC/AUC perfcurve pROC::roc(), PRROC::roc()
Confusion Matrix confusionmat caret::confusionMatrix()

[!TIP] Key Concept: Regularization (Ridge/Lasso) adds penalty term $\lambda$ to loss function to prevent overfitting. Lasso (L1) can drive coefficients to zero (feature selection).

4.2.2 Unsupervised Learning

  • Clustering:

    • k-Means: MATLAB: kmeans. R: stats::kmeans(). Sensitive to initial centroids & scale.

    • Hierarchical: MATLAB: linkage, cluster. R: hclust(), cutree().

    • DBSCAN: MATLAB: dbscan (Statistics Toolbox). R: dbscan::dbscan().

  • Dimensionality Reduction:

    • PCA: MATLAB: pca. R: prcomp() (recommended) or princomp().

    • t-SNE: MATLAB: tsne. R: Rtsne::Rtsne() or uwot::umap (for UMAP).

4.2.3 Time Series Analysis & Forecasting

  • Stationarity Tests: ADF test (adftest in MATLAB, tseries::adf.test() in R).

  • ARIMA/SARIMA:

    • Model Form: ARIMA$(p,d,q)$: $$\displaystyle \phi(B)(1-B)^d y_t = \theta(B)\epsilon_t $$, where $B$ is backshift operator.

    • MATLAB: arima(p,d,q), estimate, forecast.

    • R: forecast package: auto.arima() (automatically selects p,d,q). fable/tsibble (tidyverts): modern framework.

[!TIP] Common Pitfall: auto.arima in R uses stepwise search by default; set stepwise=FALSE for exhaustive search. Always check residuals (checkresiduals() in forecast).


4.3 Simulation & Optimization Techniques

4.3.1 Monte Carlo Methods

  • Core Idea: Use repeated random sampling to estimate numerical results.

  • Random Number Generation:

    • MATLAB: rand, randn, randi (control with rng).

    • R: runif, rnorm, sample (control with set.seed).

  • Variance Reduction: Antithetic variates, control variates, importance sampling.

  • Application Example (Option Pricing):

$$S_T = S_0 \exp\left((r - \frac{\sigma^2}{2})T + \sigma\sqrt{T}Z\right)$$

where $Z \sim N(0,1)$. Payoff = $$\displaystyle \max(S_T - K, 0) $$. Discount and average.

4.3.2 Deterministic Optimization

  • Linear Programming (LP): Minimize $$\displaystyle c^Tx $$ s.t. $Ax \leq b$, $x \geq 0$.

    • MATLAB: linprog(f, A, b).

    • R: lpSolve::lp(direction="min", objective.in=f, const.mat=A, const.dir="<=", const.rhs=b).

  • Integer Programming (IP): Add integrality constraints on some $$\displaystyle x_i $$.

    • MATLAB: intlinprog(f, intcon, A, b, Aeq, beq, lb, ub).

    • R: ompr::MIPModel() + ROI plugin (more modern) or lpSolve.

4.3.3 Nonlinear & Multi-objective Optimization

  • Nonlinear:

    • MATLAB: fmincon (constrained), fminunc (unconstrained).

    • R: nloptr::nloptr().

  • Global/Heuristic:

    • Genetic Algorithm (GA): MATLAB: ga. R: GA::ga().
  • Multi-objective:

    • Pareto Front: MATLAB: paretosearch. R: mco::nsga2().

4.4 Advanced Visualization & Reporting

4.4.1 MATLAB Graphics Deep Dive

  • Customization: title, xlabel, ylabel, legend, grid on. Colormaps: colormap('jet'), parula.

  • Interactive Figures:

    • datacursormode(gcf, 'on'): Data cursor.

    • uicontrol: Create buttons/sliders. Write callback functions that update plot data.

  • Geographic Plots: Requires Mapping Toolbox. geobasemap, geoplot(lat,lon), geoscatter.

4.4.2 R Visualization Ecosystem

  • ggplot2 Grammar: ggplot(data, aes(x,y)) + geom_point() + geom_line() + labs() + theme_minimal().

    • Layers: Geoms (geom_*), Stats (stat_*), Position (position_*).

    • Scales: scale_x_log10(), scale_color_manual().

    • Faceting: facet_wrap(~var) or facet_grid(row~col).

  • Interactive:

    • plotly: ggplotly(p) converts ggplot to interactive.

    • shiny: Build reactive web apps. Core: ui (layout), server (logic), shinyApp(ui, server).

  • Dashboards: flexdashboard (R Markdown based) or shinydashboard (Shiny UI functions).


4.5 Performance, Debugging & Code Management

4.5.1 Profiling & Acceleration

Technique MATLAB R
Profiling profile on/off, profile viewer. profvis::profvis(), Rprof().
Timing tic/toc. system.time(), microbenchmark::microbenchmark().
Vectorization Replace for loops with array operations. Use vectorized functions (+, *, apply family).
Preallocation A = zeros(n,m) before loop. vector(mode="numeric", length=n) or numeric(n).
Parallel Computing parfor (Parallel Computing Toolbox). parallel::mclapply(), future.apply, foreach + doParallel.
Compiled Code MEX files (C/C++/Fortran). Rcpp: Integrate C++ for speed.

4.5.2 Debugging Strategies

  • MATLAB:

    • Set breakpoints in Editor.

    • dbstop if error: Stops on error.

    • dbstack: Show call stack.

    • keyboard: Pauses execution, enters debug mode.

  • R:

    • browser(): Insert in code to pause.

    • traceback(): After error, shows call stack.

    • debugonce(fun): Step through function once.

    • RStudio: "Debug" pane, breakpoints, environment inspection.

4.5.3 Code Management & Reproducibility

  • Version Control: Git (conceptual). Use .gitignore for large data/compiled files.

  • Packages/Libraries:

    • MATLAB: Create toolboxes. Add via matlab.addons.install. Use matlab.addons.toolbox.installToolbox.

    • R: Package development with devtools/usethis. Document with roxygen2. Use renv::init() for dependency snapshot (like pipenv/conda).

  • Reproducibility: Set seeds (rng in MATLAB, set.seed in R). Use project-oriented workflows (RStudio Projects, MATLAB Projects).


4.6 Interoperability & Specialized Applications

4.6.1 Calling R from MATLAB & Vice Versa

  • MATLAB → R: Use system command to run Rscript.

    
    system('Rscript myscript.R arg1 arg2');
    
    

    Exchange data via files (.csv, .RData, .mat).

  • R → MATLAB:

    • R.matlab package (R): readMat("file.mat"), writeMat().

    • reticulate (R → Python): Can indirectly exchange if Python has MATLAB engine.

    • MATLAB Engine for Python: From R, use reticulate to call Python which uses MATLAB Engine API.

4.6.2 Domain-Specific Application Patterns

Domain MATLAB R
Engineering Systems Simulink: Block diagram simulation. <br> Control System Toolbox: tf, pid, bode. Less common. control package: tf(), step().
Bioinformatics/Stats Bioinformatics Toolbox. Bioconductor: Large repository for genomics (GenomicRanges, DESeq2).
Image/Signal Processing Image Processing Toolbox (IPT): imfilter, bwareaopen, regionprops. <br> Signal Processing Toolbox: filter, fft. imager, EBImage (Bioconductor). signal package.

4.7 Deployment & Sharing of Results

4.7.1 Creating Standalone Applications

  • MATLAB:

    • MATLAB Compiler (mcc): Package apps into standalone executables or shared libraries.

    • MATLAB Web App Server: Deploy apps as web apps (requires Compiler & Web App Server).

    • App Designer: Build GUI apps.

  • R:

    • Shiny Apps: Deploy to shinyapps.io, RStudio Connect, or Shiny Server (open-source/pro).

    • APIs: plumber package. Decorate R functions with @* annotations to create REST API.

4.7.2 Generating Reports & Publications

  • MATLAB:

    • Live Scripts (.mlx): Combine code, output, formatted text.

    • Publish: publish('script.m', 'pdf') or 'html'. Uses markup in comments (%% sections).

  • R:

    • R Markdown (.Rmd): Code chunks ({r}), inline R (r expr). Knit to HTML/PDF/Word.

    • Quarto: Next-gen, supports R, Python, Julia. More publishing-focused.

    • Key Feature: Dynamic report generation—code runs, results (tables, plots) embedded automatically.

[!TIP] Critical Difference: R Markdown/Quarto separates code and narrative clearly in a single file. MATLAB Live Scripts are more integrated but less portable. For reproducible research, R Markdown/Quarto is industry standard.

Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in