# Matlab in a Nutshell

### Contact

Prof. Dr. Jutta Kretzberg
Tel.: +49-(0)441-798-3314
E-Mail: jumm9htta.kredritzberg@uol.d6mayepg
Office: W4-0-078

## Matlab Syntax in a nutshell

### Matrix handling

 v=[0.1 8.5 16] generates a vector M=[1 2 3; 4 5 6] generates a 2x3 matrix v2=9:99 generates vector counting from 9 to 99 Z=zeros(n,m) generates a nxm matrix Z filled with zeros M7=[] generates an empty matrix m=M(2,1) element in 2nd row and 1st column m2=M3(2,2:5) vector containing 2nd to 5th element of 2nd row r=M(2,:) all elements of 2nd row M2=[M;v] concatenates v to matrix M MT=M2' transpose matrix M2 s=size(M,d) gives back the number of elements in dimension matrix M B=repmat(M,n,m) In B the matrix M is concatenated n times vertically and m times horizontally B=reshape(M,m,n) reshapes a matrix M with m*n elements to a mxn-matrix M2=squeeze(M3) removes singleton dimensions from matrix M3 but keeps all elements

### Some basic matrix operations

 M3=M1+M2 sum of matrices with equal dimensions M3=M1-M2 sum of matrices with equal dimensions M3=M1*M2 matrix multiplication, dimensions have to match M3=M1.*M2 element-wise multiplication of matrices with equal dimensions M5=s*M2 multiplication of a scalar value and a matrix M5=M2+s adds a scalar value s to each matrix element M8=M4.^n each element of M4 is taken to the power of n S=sqrt(M) element-wise square root m=mean(M,d) calculation of the mean values in dimension d s=std(M,d) calculation of the standard deviation in dimension d v=var(M,d) calculation of the variance in dimension d s=sum(M,d) sum of elements in dimension d

### Logical operations and comparisons

 c=a&b logical AND (for matrices element-wise), c is a logical variable (1: true, 0: false) c=a|b logical OR (for matrices element-wise), c is a logical variable c=~a logical NOT (for matrices element-wise), c is a logical variable c=(a==b) c is a logical variable indicating if a is equal to b a>b, a=b accordingly

 load filename.mat loads all Matlab variables stored in file.mat into the workspace load -ascii filename.dat loads contents of ascii file file.dat into the workspace save filename saves all workspace variables to the file filename.mat save filename var1 var2 saves only variables var1 and var2 to the file filename.mat save filename.dat var1 -ascii saves variable var1 to ascii-file filename.dat

### Plotting

 plot(v) plots vector v, using the vector indices as x-values plot(x,y) plots vector y versus vector x plot(x1,y1,x2,y2) plots vector y1 versus x1 and y2 versus x2 in the same graph plot(x,y,'r-o') plots with a red line and dots for each data point (please refer to Matlab help for more graphics format options errorbar(x,y,e) plots x versus y with error bars e xlabel('bla') text for x-axis (ylabel accordingly) imagesc(M) displays matrix elements of M graphically color coded, scaled to range between minimum value (blue) and maximum value (red) of matrix elements figure opens new figure window close n closes figure window with number n close all closes all figure windows

### Random numbers

 r=rand(n,m) generates a nxm-matrix of equally distributed random numbers between 0 and 1 r=randn(n,m) generates a nxm-matrix of normally distributed random numbers with mean value 0 and standard deviation 1 r=randperm(n) vector of all integer numbers between 1 and n in random order

### Control structures

 if condition1   commands elseif condition2   commands else   commands end several elseif statements can be combined for z=1:n   commands end counting loop x=10 while x==10    commands    x=z/10 end while loop condition needs to be initialized and to be able to change in loop body

### Scripts and functions

 script body: contains commands like on command line. Saved as script_name.m call a script: script_name calls the script, all commands are executed function body: head:function [out1,out2,out3]=function_name(in1,in2,in3) input and output arguments are optional. Save as function_name.m function call: [out1,out2,out3]=function_name(in1,in2,in3) outputs are stored in separate variables out1, out2, out3
Webmaster: Jutta6g8 Krtwetzberg (jutta.wftjkretzberg@uol.demctme) (Changed: 2020-01-23)