MATLAB

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Output if file doesn't exist

-1

algorithm for using a file

1. open the file 2. read, write, or append from the file 3. close the file.

abs(-5)

5

plus(2,5)

7

Trace Table

A listing of the variables used in the program. Completed by writing down each value that the program assigns to a variable

fprintf( 'text', variable.....)

Allows you to format variable in a certain way. More control than disp( )

control characters

Are not printed but accomplish a task (backspace or tab)

fgetl vs fgets

Both read strings from a file one line at a time. fgets keeps any newline characters, whereas fgetl does not

reshape(A)

Changes the dimensions but keeps the same number of elements

ones(2,3)

Creates a 2 by 3 matrix of 1's

zeros(4,2)

Creates a 4 by 2 matrix of zeros

magic function M = magic(3)

Creates a matrix where the sum of each column and each row are the same. (can only have one input)

rand(n)

Creates a random matrix (nxn) of all real numbers between 0 and 1

length(A)

Determines largest dimension of matrix A

size(K)

Determines number of rows and columns in matrix K

diag(A)

Extracts all diagonal elements from a matrix

subplot(m,n,p) or subplot(mnp) or subplot mnp

Figure window is divided into m x n rectangular subplots arranged like elements in an m x n matrix. p specifies the pth window for the current plot. Numbered left to right, top to bottom

%f %e %g %d

For fprinf %f= float (decimal) %d = integer (stands for decimal integer) %c= character (one character) %s= string of characters

When does a for loop end?

For loop breaks when the loop variable exceeds the terminating value.

randn(n,m)

Gives a matrix of normally distributed random numbers

A'

Gives the transpose of matrix (changes rows to columns)

Symmetric matrix

Matrix is the same as its transpose

fprintf('___________%5.4f\n',......)

Prints a string where the variable is a float with a field width of 5 and 4 decimal places for the variable where..... would be.

How are strings enclosed?

Single quotes

empty vector [ ]

To delete an element from a vector, assign an empty vector to that element

/n

Used in fprintf statement to create a new line

vertical concatenation

Using [ ; ] semicolon separates char all need to be the same length (can use spaces), but char does this automatically

When to use a while loop

When we do not know in advance how many times we will repeat executable statements

A= 1 2 3 4 5 6 7 8 9 10 11 12 0 4 5 6 a) Extract 3rd column b) Extract 2nd & 3rd column c) Extract 2nd & 3rd rows

a) A(:,3) b) B(:,2:3) c) C(2:2:4,:)

Leading blanks

blank spaces at the beginning of a string

trailing blanks

blank spaces at the end of a string

mean(data)

calculates the average

median3Filter

calculates the median of every three values in a vector. First and last elements are left alone

fclose

closeresult = fclose(fid) closeresult = fclose('all') 0 if closes successfully, -1 if not

union(a,b)

combines the data set from vectors a and b with no repeats

strcmp(s1,s2)

compares each element of s1 to the same element of s2, returns 1 for true if they are identical and 0 if false

strncmpi(s1,s2,n)

compares n characters, ignoring case

strcmpi(s1, s2)

compares strings and ignores upper or lower case

strncmp(s1,s2,n)

compares the first n characters of strings s1 and s2. Returns 1 if they are identical, 0 if false.

str2num

convert string to number

lower

converts a string to all lowercase

upper

converts all the characters of a string to all upper case

num2str

converts number to string

hist(vec,n) or histogram(vec,n)

creates a histogram which shows the frequency of values within a vector. n = the number of bins, by default, n = 10

loglog(x,y)

creates a plot with a logarithmic scale on both axes.

semilogy(x,y)

creates a plot with a logarithmic scale on the y-axis and linear scale for the x axis

blanks(5)

creates a string of all blank spaces, in this case, 5 blank spaces

strtrim

deletes both leading and trailing blanks from a string

mode(x)

determines the number that appears most frequently in x. If there's more than one, it takes the smallest.

movie(M)

displays recorded movie frames. Frames are first captured in a loop using the built-in function getframe, and are stored in a matrix m.

pie(x)

displays the percentage that each element in vector or matrix x contributes to the sum of all elements.

bar(x,y)

displays values of y as the vertical bars, x must not have duplicate values

Order in which values in a matrix are extracted and stored

down columns. to extract and store by rows, you must use a transpose. Don't forget that matrices are CREATED row wise

barh(x,y)

draws a horizontal bar chart

stem(x,y)

draws a stem plot

area(x,y)

draws the plot as a continuous curve and fills in under the curve that is created

sort in a matrix

each individual column is sorted ex) sort(mat,2) -sorts on rows instead of columns

comet(x,y)

end result is like plot(x,y), but it displays on point at a time

xlswrite

exports data as an excel file

general form used for fopen

fid = fopen('filename', 'permission string'); permission string will be r for reading, w for writing, or a for appending.

strrep(string, oldsubstring, newsubstring)

finds all occurrences of the old substring withing the string, and replaces it with the new substring

findstr(s1,s2)

finds one string in another string, returns a vector of indices of the beginning of the strings, empty vector if none

Format of a for loop

for loop_var = first_value [:step] : last_value

General for for fscanf

fscanf(fid, 'format', [dimensions]) If there are both numbers and letters, characters will be converted to their ASCII equivalents

creating a function

function a = name_of_function(x)

grid

function that puts a grid on the graph, makes it easier to see data points.

semilogx(x,y)

generates a plot of the values of x and y, using a logarithmic scale for x and a linear scale for y

mean(x)

gives the average in the x set of numbers

Getting comments from your user made function

help name_of_function

What do you need to add for a string input?

input('Enter your name: ', 's')

linspace(x,y,n)

linspace(initial, final, number of elements) Calculates the space between each element

to read variables from a MAT-file into the base workspace

load filename variable list

outlier

number much larger or smaller than the rest of the data set, usually the result of an error

when to use load Filename.ext

only if a file has the same number of values on every line and the same type

setting a line width in a graph

plot(x, y, 'LineWidth', 3)

format for graphing

plot(x-var,y-var,line marker & line) Ex) plot(x,y, 'r--o')

Name 6 3D equivalent plot functions to 2D

plot3, bar3, bar3h, stem3, pie3, comet3

r = 1 3 5 -1 -3 -5 -7 Pull out 1,5,-3,-7

r(1:2:7) 1= first entry, 2= skip every 2, 7= 7th entry

randi([_________],____,____)

randi([range],n,m)

xlsread

reads data from an excel file

fscanf

reads formatted data into a matrix, using conversion formats such as %d for integers, %s for strings, %f for floats

textscan

reads text data from a file onto column vectors stored in a cell array, also uses conversion format

deblank

removes trailing blanks

r = [1:2:6, -1:-2:-7]

returns r= 1 3 5 -1 -3 -5 -7 Vector = [ initial: skip by: end]

unique(a)

returns all data in array a without any repetitions

rand(n,m)

returns an array of random numbers with n rows and m columns

length(s)

returns number of characters in a string

intersect(a,b)

returns the data common from a and b, no repetitions

setxor(a,b)

returns the data from a and b that has nothing in common, no repetition. Finds what occurs in a or b, but not both.

setdiff(a,b)

returns the data in a that is not in b, with no repetitions and sorted in order

to append a variable from a file

save -append filename variablename

How to save a file

save Filename MatrixVariableName -ascii or save Filename MatrixVariableName -ascii -append

to save all workspace variables in a file

save filename

to save just one variable

save filename variablename

sortrows

sort the rows within a column vector

sort

sorts a vector in ascending order(default) or descending order to sort descending: sort(vec, 'descending')

sort in a cell array of strings

sorts strings in alphabetical order

white space characters

space, tab, newline(\n), and carriage return

fopen

specifies the name of the file and the mode (reading/writing/appending) default is reading. always assign to a variable

std(x)

standard deviation of array x equal to the square root of the variance

cell array

stores different types of values in its elements, could be a vector or a matrix. Stores strings of different lengths. Also uses { } instead of [ ].

Horizontal Concatenation

strcat('word', 'other word') or ['word' 'other word'] *If there are trailing blanks, only [ ]will keep them

switch statement

switch case case otherwise end

median(x)

takes the number in the middle of a sorted data set. If theres an even amount, it take the average of the two middle values.

labeling a graph

title('Title of Graph')

isletter

true if the input argument is a letter of the alphabet

ischar

true if the input argument is a string or a character vector

isspace

true if the input argument is a white space character

concatenating matrices horizontally (side-by-side)

v = [A B] where A is a 3 by 2 matrix and B is a 4 by 2 matrix. They must have the same number of columns to be put side by side

concatenating matrices vertically (one on top of another)

v = [C; D] where A is a 5 by 2 matrix and B is a 5 by 3 matrix. They must have the same number of columns to be put side by side

menu function

variable = menu('Title', 'choice 1', 'choice2'...)

var(x)

variance of the variable X

when to use for loop

when we know in advance how many iterations there will be or how many times we will repeat executable statements also used if you want to repeat the process of prompting the user and reading input a specified number of times

feof(fid)

would return logical true if the end of the file has not been reached for the file identified by "fid", or logical false if not (0)

labeling a x or y-axis

xlabel('X-axis title') and ylabel('Y-axis title')


संबंधित स्टडी सेट्स

FSHN150: Chapter 2: Designing a Healthy Eating Pattern

View Set

Abeka 7th grade Science appendix quiz R

View Set

Patho 230 midterm 2 (quizzes, reviews)

View Set

5.3 Modern Genetics Uses DNA Technology

View Set