Vous êtes sur la page 1sur 5

Text File Reading

fprintf writes to a text file rather than the command window


fopen(filename, read or write) sets a variable equal the the file.
fgets(file) gets the one line and the new line break
fgetl(file) gets the one line
ischar(stuff) Boolean value of it stuff has a char or not


double(str)
converts a string into an array of its ASCII values

char(arrayOfNums)
converts from doubles to characters

strcmp(string1, string2)
tells you if theyre the same
look up what strcmpl or strcmpi is

all(blah == blahhh)
output will tell you if theyre all true, 0 if theyre not all true, 1 if theyre all true

linspace(a, b, c)
this splits the space between a and b into c parts

round(a)
rounds a function

ceil(a)
rounds a number up

floor(a)
rounds a number down

colon operator:
a:b
this creates a vector from space a to b
suppose a = 1 and b = 5, the output is
1 2 3 4 5
length(a)
calls then length of the vector

double colon operator:
a:b:c
creates a vector space from a to c with step
suppose a = 1, b = 3, c = 20, the output is
1 4 7 10 13 16 19

zeroes(a, b)
creates a matrix of size a x b with zero in all places

ones(a, b)
same as above except 1s

multiplying a matrix by a number
multiplies each value by the number

rand(a,b)
creates a matrix by size a x b and puts pseudorandom numbers in spaces

Math operators

+
adds shit
.*
Multiplies shit on a 1 by 1 basis (elements of the same space multiplied)
*
crossing matricies
transpose
add after a matrix to get its transpose
sum(a .* b)
multiplies the matrices and then gives you the sum of the values
vectorB= vectorA(1:2:end)
creates vector B from vector As first value, then every second value after the first until the end
flip a vector
flip = vec(end:-1:1)

vector operators:
vec1 + 5
adds 5 to every element of the vector
works the same for subtraction
[a,b] = max(vec1)
a = the max value
b = the position of the max value
[a,b] = min(vec1)
a = min value
b= the position
sum sums each value in the vector
prod outputs the product of all the values in the vector
mean outputs mean of all the values in the vector
mod(vec1, 2) -> a mods each value of vec1 by 2
[a,b] = sort([2,4,1,3])
a = [1,2,3,4] (this is the numbers in order)
b = [3,1,4,2] (this is the corresponding index of each value

index operators with vectors
vec1 = [3,5,6,1,2]
vec(3) outputs 6
vec4 = 10
-> sets the fourth value to 10
vec([1 3])
outputs [3 6]
vec([1 3 ] = 2
reassigns the values to 2
vec([1 3]) = [9 10]
changes value in index 1 to 9 and index 3 to 10
vec([2:4]] = [5,3,12]
changes values from index 2 to 4 to the values requested
vec

Matrix operators
a = length(c) -> returns the larger dimention of the matrix
a = round(rand(3,4).*12) -> creates a 3x4 matrix with random numbers
a = size (matrixA) -> returns rows columns
a = magic(7) -> creates a 7x7 matrix where all the columns and rows equal the same number
colsum = sum(vecA)
rowsum = sum(vecA)
diagonalValues = diag(a)

if you get a matrix of Boolean values to say which ones are bigger and want to output a matrix with
those numbers
make a matrix = zeros(size of matrix, but for example 3, 4)*NaN
maxtrixOfBiggers(where a is greater than b A>B) = A(A>B)

Matrix indexing

Arr(1,4)
calls the value in row 1 column 4
Arr(1:2, 2:3)
creates a 2x2 matrix that includes the values that are in the first and second row and the second
and third column
Arr(:)
creates a column vector with all the values
linear indexing the index of all the maxtrix values go top to bottom, left to right starting from 1
Arr(:,2)
outputs every rows second number)
Arr(:,4) = [3;4;5] or [3
4
5]
sets the values in every rows fourth column to these numbers (assuming the matrix is a 3x3)`
mask = Arr==2
creates a matrix mask with 0 or false where there arent twos and 1 where there is a 2
b = A2(mask)
creates a vector with the values of A2 where mask is true
A2(mask) = Arr(mask)
turns all the values of A2 where mask is true to the values of Arr where mask is true

logical operators
bigger = vecA > vecB
outputs a matrix with 1s in all the places vecA is bigger
vecC = vecA(bigger)
creates vec with places there is a 1

Stuff with images
click on image to get coordinates
image = imread( imagename with .jpg or whatever ending)
max(max(image))
gives the max value of the image
imshow(Image)
shows the image

lookupwhat figure means
to stretch and image gather the part you want (the size of it)
ndx = round(linspace(1,mc, mc *2.5));
middle middle(:, ndx);
imsho(middle)
sptLimo = [front, middle, back]
figure
imshow(sprtLimo)

expanding a part of an image
partToExpand <- renamed ed= (161, 53, 185, 84);
wheel = car(ed(2):ed(4), ed(1):ed((3));
figure
imshow
[wh, ww] = size(wheel);
bh = wh .*2.5;
bw = ww .* 2.5;
now to put it somewhere else
topLeftCol = 170;
topLeftRow = 100;
rowNdx = tlR:tolR + bh-1;
colNdx = tlC:tlC+bw-1;
car(rowNdx, colNdx) = 100

bigWRowNdx = round(linspace(1,wh, bh));
bigWColNdx = round(linspace(1,ww, bw));

bigW = wheel(bigWRowNdx, bigWColNdx);
figure
imshow(bigW)

car(rowNdx, colNdx) = bigW;
figure
imshow(car)






string formatting
%d = double
%s = string
\n break
fprintf(blajfdsjs %s fldlsalfdsla %d\n, str, num);
outputs blajfdsjs str fldlsalfdsla num <- and then the line breaks
sprintf(formatting stuff, stuff going in)
use this to store this into a variable
take a string, subtract a then add A to convert to capitals
find(Boolean) = outputs the index of where things are true
spaces = find(str== )
firsts = [spaces +1, 1]
str(firsts) = str(firsts) + A a
*token, rest+ = strtok(str, )
outputs the string before the token and the rest of it

case structures
month = input(which month )
switch month
case (4, 6, 9, 11)
days = 30;
case 2
days = 28
case(1,3,5,7,8,10,12
days= 31
otherwise
error(bad month)
end
fprintf(Month %d has %d days/n, month, days);

Vous aimerez peut-être aussi