You are on page 1of 3

>> homeRow = 4;

>> homeCol = 8;
An array can be a list of numbers and is created by
>> locRows = [6;2;8];
enclosing the list in square brackets and separating the >> locCols = [5;7;2];
elements with semicolons.

You can create a cell array to hold words using curly >> locNames =
braces. {'Mall','Movies','Cafe'};

>> distRows = abs(locRows -


homeRow)
distRows =
2
2
4
>> distCols = abs(locCols -
homeCol)
distCols =
Perform calculations on arrays. 3
1
6
>> distTotal = distRows +
distCols
distTotal =
5
3
10

>> minDist = min(distTotal)


Calculate the minimum distance. minDist =
3

>> [minDist, minIdx] =


min(distTotal)
Calculate the minimum distance and corresponding minDist =
index. 3
minIdx =
2
>> closestLoc =
locNames(minIdx)
closestLoc =
'Movies'
Use generated index to extract information from other >> moviesRow = locRows(minIdx)
vectors moviesRow =
2
>> moviesCol = locCols(minIdx)
moviesCol =
7

>> homeRow = 4
Variable names are entered on the left, followed by the equals homeRow =
sign, then the value to assign the variable. 4
>> locName = 'Mall'
If the variable contains letters, enclose it in single quotation locName =
marks. Mall

>> homeRow
You can print out the variable value in the Command Window homeRow =
by typing just its name. 4

clear cleans up the variables from the Workspace. >> clear

clc cleans up the text in the Command Window. >> clc

>> homeRow = 4;
>> homeCol = 8;
You can suppress the output from being printed on the >> locRow = 6;
Command Window by ending the command with a semicolon. >> locCol = 5;
>> locName = 'Mall';

>> distRow =
abs(homeRow - locRow);
>> distCol =
You can perform math using variables. abs(homeCol - locCol);
>> distTotal = distRow
+ distCol;

Remember that you can use the up arrow, , to recall previous commands.