Texts: Carlson Reserves, on-line tutorials, used copies of Attaway Text...
Run matlab, play tutorial. Windows, editor, help, completion, etc. Running from your computer: see X11 Forwarding with SSH.
Matlab is horrifically interpreted, flexible, polymorphous, transgendered, default- and convention ridden, fickle. Total triumph of engineering opportunism over elegance and simplicity. HOWEVER, it usually does what you want, AND if anything seems complicated you're probably not going about it the cutest way.
>> help ones
ONES Ones array.
ONES(N) is an N-by-N matrix of ones.
ONES(M,N) or ONES([M,N]) is an M-by-N
matrix of ones.
ONES(M,N,P,...) or ONES([M N P ...])
is an M-by-N-by-P-by-... array of ones.
ONES(SIZE(A)) is same size as A, all ones.
ONES with no arguments is the scalar 1.
ONES(M,N,...,CLASSNAME) or
ONES([M,N,...],CLASSNAME) is an M-by-N-by-...
array of ones of class CLASSNAME.
e.g. x = ones(2,3,'int8');
Note: The size inputs M, N, and P...
should be nonnegative integers.
Negative integers are treated as 0.
See also eye, zeros:
Reference page in Help browser: doc ones
It gets worse. max exhibits several typical Matlab family traits...
MAX Largest component. For vectors, MAX(X) is the largest element in X. For matrices, MAX(X) is a row vector containing the maximum element from each column. For N-D arrays, MAX(X) operates along the first non-singleton dimension. [Y,I] = MAX(X) returns the indices of the maximum values in vector I. If the values along the first non-singleton dimension contain more than one maximal element, the index of the first one is returned. MAX(X,Y) returns an array the same size as X and Y with the largest elements taken from X or Y. Either one can be a scalar. [Y,I] = MAX(X,[],DIM) operates along the dimension DIM. When X is complex, the maximum is computed using the magnitude MAX(ABS(X)). In the case of equal magnitude elements, then the phase angle MAX(ANGLE(X)) is used. NaN's are ignored when computing the maximum. When all elements in X are NaN's, then the first one is returned as the maximum. Example: If X = [2 8 4 then max(X,[],1) is [7 8 9], 7 3 9] max(X,[],2) is [8 and max(X,5) is [5 8 5 9], 7 5 9]. See also min, median, mean, sort. Overloaded methods: timeseries/max fints/max ordinal/max codistributed/max Reference page in Help browser doc max
function cbdemo(N) %lives in file cbdemo.m
xrange = linspace(0,2*pi,N); % make vectors,
% built-in consts
yrange = linspace(0,25*pi,N);
lf_sine = sin(xrange); % vectorized functions
hf_sine = sin(yrange);
figure % new graphic window: else create and reuse
plot(lf_sine+ .2*hf_sine); % bare-bones graphics
yrange = linspace(0,8*pi,N);
mf_sine = sin(yrange);
z= zeros(N); %typical default: (N,N)
for x = 1:N % control statements
for y = 1:N
z(x,y) = lf_sine(x)+.2*mf_sine(y);
end
end
figure % new graphic window
surf(xrange,yrange,z); % bare-bones graphics
end %function
% run like:
% >> cbdemo(100)
disp('average of squares of integers from 1 to 5.');
total = 1^2+ 2^2+ 3^2+ 4^2+ 5^2;
avesq = total/5
disp('add up integers from 1 to 50.');
disp(sum(1:50)); %default increment is 1
disp('vectorization: sum squares, 1 to 5.');
avesq2= sum([1:5].^2) / 5 % using more built-in power
Teleport to CSC160 .
X = magic(4)
X =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
X(:,2) = []
X =
16 3 13
5 10 8
9 6 12
4 15 1
Following makes you appreciate information hiding, methods, abstract data types,
etc...
X(2,2)
ans = 10
% but
X(2:2)
ans = 5
WTF? As is possible in C, 2-D matrices are stored as 1-D array
(columnwise) and may be addressed that way (as in C's pointer
arithmetic).
Matlab here is forced to use a single
number (the range 2:2) as index. It gets worse of course, since
the whole horror vectorizes.
X(2:10)
% 2:10 is a range of single indices,
% so vector answer.
ans = 5 9 4 3 20 6 15 13 8
X(2:2:10)
%count by twos in range
ans = 5 4 20 15 8
if...elseif...end No then!
end terminates
for, while, switch, try, if, function,
also indicates last index of array (welcome to Matlab ;-} ).
Structuring functions:
Surely this is an early effort. Pretty convoluted if-else's! Simulates robot in an array "grid world".
% Matlab Simulator for Meccano Robot % Programmed by Olac El Terrible worldsize = 360; maxtime = 10000; world = 250*ones(worldsize,worldsize); world(20:worldsize-20,20:worldsize-20)=world(20:worldsize-20,20:worldsize-20)*0; obstacle_size = 40; tot_obstacles = 7; obstacle = 250*ones(obstacle_size,obstacle_size); start = [21; 21]; goal = [worldsize;worldsize]; obstacles = [60,60; 140,140; 220,100;140,220;220,180;260,260;60,180]; for i=1:tot_obstacles start_row = obstacles(i,1); start_col = obstacles(i,2); world(start_row:start_row+obstacle_size-1, start_col:start_col+obstacle_size-1) = obstacle; end ir_source = goal; pos = start; theta = 1; power = 5; image(world); for i=1:maxtime row=pos(1); col = pos(2); [theta power] pos sx = 5*[cos(theta+1.57);sin(theta+1.57)]; sy = 20*[cos(theta);sin(theta)]; vertices = [pos+(sx+sy),pos+sx,pos-sx,pos+(-sx+sy)]; mpl = round((vertices(:,1)+vertices(:,2))*.5); mpr = round((vertices(:,3)+vertices(:,4))*.5); rv = round(vertices); bump_fl = world(rv(1,1),rv(2,1)); bump_fr = world(rv(1,4),rv(2,4)); bump_back = world(rv(1,2),rv(2,2))+world(rv(1,3),rv(2,3)); bump_l = world(mpl(1),mpl(2)); bump_r = world(mpr(1),mpr(2)); % image(world); patch(vertices(2,:),vertices(1,:),125); drawnow; line_to_beacon = ir_source - pos; front_beacon_angle = atan2(line_to_beacon(2),line_to_beacon(1))-theta; front_beacon = max(255*cos(front_beacon_angle),0); left_beacon = max(255*cos(front_beacon_angle-1.5708),0) ; right_beacon = max(255*cos(front_beacon_angle+1.5708),0); back_beacon = max(255*cos(front_beacon_angle-pi),0); beacons = [front_beacon,left_beacon,right_beacon,back_beacon]; photo_front = world(round(row+cos(theta)*35), round(col+sin(theta)*35)); photo_left = world(round(row+cos(theta-.2)*35), round(col+sin(theta-.2)*35)); photo_right = world(round(row+cos(theta+.2)*35), round(col+sin(theta+.2)*35)); [photo_front photo_left photo_right] if bump_fl power = -5; theta = theta -.7; elseif bump_fr power = -5; theta = theta +.7; elseif bump_l theta = theta - .3; elseif bump_r theta = theta + .3; elseif bump_back power = 5; else if photo_left if photo_front theta = theta + .6; else theta = theta + .3; end else if photo_right if photo_front theta = theta - .6; else theta = theta - .3; end end end end pos = pos + power*[cos(theta);sin(theta)]; end
Please look over the content and tutorial materials for the three possible Matlab projects (Ray Tracing, Ballistics, Pivoting Research), and bring to class a piece of paper with your likely favorite written down.
Thanks!