Warren Thorngate (2000)
Teaching Social Simulation with Matlab
Journal of Artificial Societies and Social Simulation
vol. 3, no. 1,
<https://www.jasss.org/3/1/forum/1.html>
To cite articles published in the Journal of Artificial Societies and Social Simulation, please reference the above information and include paragraph numbers if necessary
Received: 16Dec99 Accepted: 25Jan00 Published: 31Jan00
The fundamentals

What to do


start Matlab  choose Matlab from the Start/Programs button at the lower left of a Windows screen 
stop Matlab  drag the mouse to the word Exit under the File menu and lift finger off clicker 
understand what a prompt is and how to find it 
go to (scroll if necessary) the bottom of the white window and look for two "greater than" or "French closequote" signs a with a blinking bar just to their right 
reduce fear of making a mistake 
when the prompt is found, press the palm of either hand down in the middle of the keyboard, then press the key marked Enter. Example: a vugbhiu768rt ??? Undefined function or variable 'vugbhiu76rt'. a 
a56.3+216 ans = 71.3000 
a3.1*[2 5 1 3] ans = 6.2000 15.5000 3.1000 9.3000 
asort([5 2 8 6 2.3 41.8 6.78 4]) ans = Columns 1 through 7 6.7800 4.0000 2.0000 2.3000 5.0000 6.0000 8.0000 Column 8 41.8000 
amean([5 2 8 6 2.3 41.8 6.78 4]) ans = 6.7900 
aceil(10*randn(3,5)) ans = 11 8 8 9 14 
In order to

Use the following command

Examples (omitting the >> prompt & extra
lines)


Make a vector "vec"and a matrix "mat" 
[ ] [ ] with semicolons to separate rows of a matrix a function which returns a vector or matrix 
vec=[34 65  61 0 5.934] mat=[4 7 3;45
9 2.1;16 2 16] mat=mod(round(10*rand(3,4)),3)

make a vector or matrix with zero in each cell, or with ones in each cell, or make a square matrix with ones down the main diagonal  zeros, ones, eye 
vec=zeros(5,1) mat=ones(2,3) mat=eye(4) 
make a vector with consecutive numbers  : (the colon) 
vec= 1:7 vec = 12:3:1 
transpose a vector or matrix  ' (the apostrophe) 
vec=[5 3 7 1] vec' vec' * vec mat=fix(10*rand(2,3)) mat' 
find the length, maximum, minimum, sum, mean, median, variance, standard deviation of a vector  length, size, max, min, sum, mean, median, var, std 
vec=[45 32 56 87
12 98 31 52] length(vec) max(vec) min(vec) sum(vec) mean(vec) median(vec) var(vec) std(vec) 
find the size, column maximum, column mean, etc. of a matrix 
size, max, mean,... (NB the last example shows how to obtain the mean of the rows of a matrix: first do a rowcolumn transformation, the ask for the column means) 
mat=[6 9 1 0;4 4
11 6;9 31 5 3] size(mat) max(mat) max(max(mat)) mean(mat) mean(mat') 
extract parts of a vector or matrix  : (colon) 
vec=[41 82 53 94
25 36 77] part=vec([5 4 3]) part=vec(3:6) mat=[11 12 13 14;21
22 23 24;31 32 33 34] part=mat([3 2],[3
4]) part=mat(:,4) part=mat(2,:) 
Matlab commands...

and the resulting Figure


a x=1:5 x = 1 2 3 4 5 a y=[27 13 98 2 21] y = 27 13 98 2 21 a plot(x,y) 

a IQ=100+(15*randn(1,1000)); a hist(IQ) 

a mymatrix=ceil(80*rand(8,8)) mymatrix = 59 55 72 43 27 65
68 77 a image(mymatrix) 

a spy(mymatrix<30) 

Matlab code and response  Explanation 
prospects=ceil(10+3*randn(1,100)) 
Generate a vector of 100 numbers (columns) sample from a normal distribution with mean  0 and standard deviation =1; multiply each number by three; add ten to each number; then take the next higher integer (ceil) of each number Results: prospect(100) has 10 desirable features 
Matlab command and response  Explanation 
goodones=find(prospects
>= 12) goodones(1) 
find the index numbers (#) of the prospects who have 12 or more features
and put the result in a variable called "goodones"

Doing the simulation by hand 
prospects=ceil(10+3*randn(1,100)); prospects=ceil(10+3*randn(1,100)); prospects=ceil(10+3*randn(1,100)); prospects=ceil(10+3*randn(1,100)); 
Matlab command 
Explanation


edit  Start the Matlab editor 
prospects=ceil(10+3*randn(1,100));
goodones=find(prospects >= 12); goodones(1) 
find all prospects with 12+ good features 
ctls (cmds for Matlab on a Mac)  save the file, using the arbitrary name: takeit 
takeit takeit takeit takeit 
Matlab command (N.B. Matlab's editor automatically indents lines between for and end statements to make the programme pretty) 
Explanation

edit takeit  Start the Matlab editor 
result=zeros(1,1000); for student=1:1000 prospects=ceil(10+3*randn(1,100)); goodones=find(prospects >= 12); result(student)=goodones(1); end mean(result) 
initialize the count
for each of 1000 trials 
ctls (cmds for Matlab on a Mac)  resave the file takeit 
The modified programme (resaved with ctls)


numfeatures= input('How many desired features is your friend looking
for? ') 

Running the modified programme


takeit takeit takeit takeit takeit takeit takeit takeit 

x=10:17 y=[1.52 2.02 2.64
3.81 6.37 10.89 20.68 43.98] plot(x,y)


When all else fails, lower your standards.

The Matlab function "neighbours" (entered
in Matlab's editor and saved as a file)


function result =
neighbours(x) north=[x(2:end,:);x(1,:)]; south=[x(end,:);x(1:end1,:)]; east=[x(:,end) x(:,1:end1)]; west=[x(:,2:end) x(:,1)]; result=north+south+east+west; 

Using the "neighbours" function to calculate
the number of North, South, East and West
neighbours in each cell of a matrix called "bugs", assuming rows and columns wrap around in torus fashion 

bugs=rand(5,4)>0.5 neighbours(bugs) 

Examples of a Matlab data cell


a
dc(1,1)={rand(3,3)} dc = [3x3 double] a dc(1,2)={'hello there'} dc = [3x3 double] 'hello there' a dc(2,1)={[54 23 76]} dc = [3x3 double] 'hello there' [1x3 double] [] a dc(2,2)={999} dc = [3x3 double] 'hello there' [1x3 double] [ 999] 
a dc{1,1} 
Examples of a Matlab data structure


a agent(1).name='mary' agent = name: Mary a agent(2)Name='fred' agent = 1x2 struct array with fields: name a agent(1).height=150 agent = 1x2 struct array with fields: name height a agent(2)Height=180; a agent(1).grades=[78 83 81]; a agent(2)Grades=[66 61 75]; a agent(1)Grades=[agent(1)Grades 89]; 
a agent(1)Grades ans = 78 83 81 89 a agent(2)Grades=[agent(2)Grades 76]; a agent(2)Grades ans = 66 61 75 76 agent.name ans = Mary ans = Fred a sum([agent.grades]) ans = 531 mean([agent.height]) ans = 165 
THORNGATE, W. (1980). Efficient decision heuristics. Behavioral Science, 25, 219225.
THORNGATE, W. (1988). On the evolution of adjudicated contests and the principle of invidious selection. Journal of Behavioral Decision Making, 1, 516.
THORNGATE, W. & Carroll, B. (1987). Why the best person rarely wins: Some embarrassing facts about contests. Simulation and Games,18, 299320.
THORNGATE, W., & Carroll, B. (1991). Tests versus contests: A theory of adjudication. In Baker, W., Hyland, M., van Hezewijk, R., & Terwee, S. (Eds.), Recent trends in theoretical psychology: Volume 2. New York: SpringerVerlag, pp. 431438.
THORNGATE, W., & Hotta, M. (1995) Life and luck: Survival of the fattest. Simulation and Gaming. 516.
THORNGATE, W., Hotta, M., & McClintock, C. (1996). Bingo! The case for cooperation revisited. In Tolman, C., Cherry, F., & Van Hejiwek, R. & Lubek, I. (Eds.), Problems of Theoretical Psychology. New York: Springer.
Return to Contents of this issue
© Copyright Journal of Artificial Societies and Social Simulation, 1998