Here are some tips for scientific computation using Matlab which may help make your coding smoother :)
Here is an example using eval
from this post:
X = 'omega';
eval([X '= [2 3 4 5]']);
In this way, we create a variable omega
which is [2 3 4 5]
.
Another example:
eval(['Result = Result',num2str(i)]);
when i = 1
, this is Result = Result1
.
Sometimes we want optional input arguments with default values. This can be done using varagin
. Here is an example from this post:
function foobar(varargin)
Defaults = {A,B,C...};
idx = ~cellfun('isempty',varargin);
Defaults(idx) = varargin(idx);
or
function foobar(varargin)
Defaults = {A,B,C...};
Defaults(1:nargin) = varargin;
Here is my code
function [K] = getKRNS_(N,varargin)
% getKRNS_ Get the K-matrix of R-NS two string
% N : Cutoff length for one string
% Optional input epsilon: Regulator
ep = 0;
if nargin == 2
ep = varargin{1};
end
pass
end
The input can be
>> K = getKRNS_(50);
>> K = getKRNS_(50,0.005);
Note:
nargin
counts all the input arguements, including the ones that are not optional.- The optional arguments don't need to be grouped in extra brackets. The sequence matters.
A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. Access data in a field using dot notation of the form structName.fieldName
. Note the field names have to be strings.
An example from this post:
function y = f(x,params)
% Something
if isfield(params,'length')
v = linspace(a,b,params.opt1);
elseif isfield(params,'step')
v = a:params.opt2:b;
end
% Something
end
y=f(42, struct('length',100));
y=f(42, struct('step',0.5));
If I want to write a function which can return the entanglement entropy (denoted as S) for bipartition, and return the entanglement entropy as well as the logarithmic negativity (LN) for tripartition.
This can be done using containers.Map
.
function[Ent] = getEnt_(Gamma)
Ent = containers.Map;
%% EE
% do something...
Ent('S') = S;
Ent('ES') = ES;
%% LN
% do something...
Ent('LN') = LN;
Ent('LNS') = LNS;
end
Function handle can save us from writing lengthy for loop, which is particularly useful if we want to generate a matrix with known expression. Here is an example:
RLis = (1:N)';
eleFun = @(n,m) 1/2.*(n-m)./(n+m).*u(2.*n+1).*u(2.*m+1);
ele = bsxfun(eleFun, RLis, RLis');
Note:
- We need to use element-wise operator in function handle
.*
User manual, and this, this. One thing worth noticing (and sometimes annoying) is:
MATLAB differs from languages like C++ and Java® in that there is no special hidden class object passed to all methods. You must pass an object of the class explicitly to the method.