Three Categories of Code
Sequences - lines of code executed one after another
Selection Structures - executes some piece of code if some known condition is true, otherwise executes some alternative.
Repetition Structures - (loops) causes a group of statements to be executed multiple times. |
Break and Continue
Break statements cause the termination of the smallest enclosing while or for loop.
Continue statements skip to the rest of loop, advancing to the next loop pass. |
Time
clock gives you the current time
etime compares between a start and end time
cputime returns cpu time since you started matlab
tic/toc works like a stopwatch |
Array vs Matrices
Array holds stuff: can hold numeric info, char data, or symbolic data. Is "an orderly grouping of information". No special properties by virtue of its existence.
Matrix is a 2D numeric array used in linear algebra. Used extensively in STEM fields and has special properties. |
|
|
To solve Ax = b
Inverse method solution = inv(A)*b
rref command A_augmented = [A b]
RREF_result = rref(A_augmented);
solution = RREF_result(:,end)
backslash solution = A\b |
Structure Arrays
A = 'We Are!'
B = [1 4; 3 2]
C = 'Penn State!'
D = single([ 1 2; 3 4])
E = {A, B, C, D} %default printing, just shows sizes.
celldisp(E) %needed to generate display
E{3} displays 'Penn State'
E{1}(1,2) %1st row, 2nd col of cell 1 w/ multilayer indexing displays e
E{2}(:) %concatenates cell 2 into column vector
N.first = 'Hello';
N.second = 'World';
disp('N is: ')
disp(N) N is: first:'Hello'; second:'World'
orderfields(N) %orders fields in ASCII dictionary
orderfields(N,O) %orders N field like O is ordered
T = 'myphrase1';
L = rmfield(L,T) % removes 'myphrase1' field from L
|
|
|
Short Answer
41. What is the difference between a matrix and 2D array?
A matrix is 2D, has special mathematical properties. An array need not be 2D,
and has no special mathematical properties, and is merely a “holder” for data.
42. Why is it a good idea to create a flowchart and pseudocode before you attempt to create a computer program?
Creating a flowchart and pseduocode before attempting to create a computer
program is a good idea because it gives you an opportunity to think your way
through the program. A builder wouldn’t start building a house without a
blueprint; it is advisable to think through your programs as well.
43. Briefly describe how one would mathematically check if a square matrix is singular. What practical implementation issues arise when implementing that into MATLAB code?
Mathematically, a matrix is invertible if detA ~= 0. In terms of
implementation, if the determinate is ≈ 0 it can be viewed as effectively singular.
You could check the determinant against some sort of tolerance to see if your
determinant is close enough to 0 to make your matrix numerically act as singular. |
|
|
3D array whose values count down
nrows = 3;
ncols = 2;
npages = 4;
B = zeros(nrows,ncols,npages);
counter = 25;
for k = 1:npages;
for i = 1:nrows;
for j = 1:ncols;
counter = counter - 1;
B(i,j,k) = counter;
end
end
end
fprintf('The first page of B is:\n')
disp(B(:,:,1))
etc
|
Short Answer
44. What is the benefit of using cell arrays to store chars instead of using character arrays?
Character arrays have to have the same number of columns in each row. Cell
arrays of chars, however, has no such restriction.
45. Briefly discuss the difference between cell arrays and structure arrays in MATLAB.
One of the more prominent differences between cell arrays and structure arrays in
MATLAB is content indexing for cells, and the use of fields for structure arrays. |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets