Plotting & Logical Indexing
WILD!
Text Data
Loops/If statements
Functions
100

How many data points do you want for curve fitting?

50

100

(What are those symbols and what do they do?)

&

The or symbol for when any true comparison will return true.

The and symbol for when all of the comparisons need to be true to return true.

100

What is different about exporting data to a .txt file in MATLAB and importing data from a .txt file in MATLAB

Importing: fscanf and 'r'

Exporting: fprintf and 'w'

BOTH: use fopen and fclose

100

How many "end"s does this code need?

while

    for

        if

        elseif

        else

            if

                for

                    if

                        function

                        while

                        for

9

100

Write the basic outline of a function with 3 inputs and 2 outputs

AND call your function

[OutName,OutName] = Name(a,b,c)

function [Out1,Out2]=Name(In1,In2,In3)

Commands or whatevs

end

200

What is logical indexing?

Using a logical array (a matrix of logical 0s and 1s) as an index to extract certain values

200

E=[0 1 3 4]

F=[1 1 3 4]

Multiply E by F 

G=E.*F

200

Which is true?

1) numel() and length() give you the same result for a vector

2) numel() and length() give you different results for matrices

3) numel() and length() give you different results for vectors

4) numel() and length() give you the same results for matrices


1 & 2

numel() gives the same result for vectors and different results for matrices

200

How many times will this loop run?

for idx=1:4

    for idy=1:3

        for idz=1:2

fprintf('Camp Rock!\n')

        end

     end

end

4*3*2=24

200

What dou you include in the comments for your function?

Format of the call, description of inputs and outputs, description of commands

300

Format of polyfit(), polyval(), and plot()

Coefficients = polyfit(x,y,order)

New_Y=polyval(Coefficients, x)

plot(x,New_Y)

300

Are these valid or not?

3<A<8

A>3 & A<2

A>3 | A<2


NO

NO

YES

300

Where must files be on your computer in order for MATLAB to open them?

Where do they show up on MATLAB?

They must be in the correct file path open on MATLAB

They generally show up on the top left of your screen in the "Files" window. You can also click on them to open them in your script area.

300

Fix this code (STILL USING A WHILE LOOP):

while idx=1:3

    fprintf('I like %i\n',idx)

end

idx=1;

while idx<=3

    fprintf('I like %i\n',idx)

    idx=idx+1;

end

300

Create and call a function that finds the average of an inputted set of data (NOT USING THE mean command)

Comments not necessary

B=FindAvg(A)


function Avg=FindAvg(Vec)

Avg=sum(Vec)/length(Vec);

end

400

Given a vector B use logical indexing to create a new vector C which has the B values that are not equal to the mode and are less than the mean

C=B(B~=mode(B) & B<mean(B))

400

BONUS!!

What is my (Halle's) favorite band??

Queen or Abba

400

FavChristmasMovie=Die Hard

Is FavChristmasMovie a string or character array based on the following output:

strlength(FavChristmasMovie) = 8

length(FavChristmasMovie) = 8

size(FavChristmasMovie) = 1   8

Character array

400

Given a vector F

Write code that will determine how many numbers are equal to 4, less than four, and greater than four

Fours=0;

Big=0;

Lil=0;


for idx=1:length(F)

    if F(idx)==4

        Fours=Fours+1;

    elseif F(idx)>4

        Big=Big+1;

    elseif F(idx)<4

        Lil=Lil+1;

    end

end

400

Create a useful function that has no output or input

Bunch of possibilities:

function ccc

clc

close all

clear

end

500

Make a graph of vectors X and Y which adheres to ENGR 130 style guidelines and plots BOTH the individual points and a curve fit in different colors. (The equation of Y is Y=-X^3+2x^2)

X=[1:5];

Y=-X.^3+2.*X.^2;


Coeff=polyfit(X,Y,3);


Big_X=linspace(X(1),X(end),50);


New_Y=polyval(Coeff,Big_X);


hold on

plot(X,Y,'*r','LineWidth',3)

plot(Big_X, New_Y,'--b','LineWidth',3)

hold off


title('Cool title')

xlabel('X label (units)')

ylabel('Y label (units)')

legend('Points','Fit')

500

Given a vector of unknown (even length) create a matrix with the first half of the vector in row 1 and the second half of the vector in row two

B(1,:)=[A(1):A(length(A)/2)]

B(2,:)=[A(length(A)/2+1):A(length(A))]

500

The four commands used with .txt files AND their inputs

ID=fopen('FileName.txt','r/w')

fprintf(ID,'Hey %i',VarName)

fscanf(ID,'%i', #)

fclose(ID)

500

Create code for a game that allows a user to input a letter and if they enter a b they win and if they enter anything else they try again.

Gus='w';


while Gus~='B' & Gus~='b'

    Gus=input('Guess a letter:\n','s');

    if Gus == 'B' | Gus=='b'

        fprintf('You won!\n')

        break

    else

        fprintf('Try again :(\n')

    end

end

500

Create and call a function that outputs the volume and surface area of a rectangular prism given the dimensions

[Vol, SA]=Calcs(1,2,3)

function [Volume,SurfArea]=Calcs(a,b,c)

Volume=a.*b.*c;

SurfArea=2*(a.*b)+2*(b.*c)+2*(c.*a);

end

M
e
n
u