Intro 3/4 Flashcards
(36 cards)
How do you clear all variables from the MATLAB workspace?
Use the command: clear all
What does MATLAB return for logical comparisons?
MATLAB returns 1 for true and 0 for false.
What will the following MATLAB commands return?
x = 5;
x > 7
x ~= 2
x > 7 returns 0 (false).
x ~= 2 returns 1 (true).
How can you apply logical operators to an entire array?
Use element-wise logical comparisons.
Example:
A = [1, 5, 3, 4, 8, 3];
B = A > 2 % Returns a logical array
C = A < 5
What do &, |, and ~ mean in MATLAB?
& (AND): Both conditions must be true.
| (OR): At least one condition must be true.
~ (NOT): Inverts the logical value.
What does the following command do?
D = B & C;
It creates a logical vector where both B and C are true (i.e., where elements in A are greater than 2 and less than 5).
How can you use a logical array as an index in MATLAB?
E = A(D);
This selects elements from A where D is true.
What does find(D) return?
The indices of elements in D that are true.
How do you extract all values in data that belong to category 2 in cat?
data2 = data(cat == 2);
What function would you use to compute the mean while ignoring NaN values?
nanmean(data);
How do you replace specific values in an array with NaN in MATLAB?
data(err == 1) = NaN;
How do you create a logical vector for trials where reaction time (rt) was too fast (<100 ms) or too slow (>1000 ms)?
error = (rt < 100) | (rt > 1000);
How do you count the number of error trials?
sum(error);
How do you compute the mean reaction time for valid and correct trials only?
mean(rt(valid & ~error));
How do you check if reaction times were faster for targets on the left (side == 1) or right (side == 2)?
mean(rt(side == 1)) % Left
mean(rt(side == 2)) % Right
How do you correct specific values in a matrix? (Example: Fix incorrect responses for subject 2, stimuli 3, 5, and 17.)
data(stimno == 3,2) = 30;
data(stimno == 5,2) = 62;
data(stimno == 17,2) = 35;
What commands should you run at the start of a MATLAB script to clear the workspace and command window?
clear
clc
close all
This removes all variables, clears text from the command window, and closes all figures.
How do you generate a sequence of numbers with a specified length in MATLAB?
Use the linspace function.
Example:
x = linspace(0, 2*pi, 30);
This creates 30 evenly spaced numbers between 0 and 2π.
What does the following command do?
help linspace
It displays the documentation for the linspace function, explaining its syntax and usage.
What MATLAB function is used for plotting vectors as lines or points?
The plot function.
Example:
plot (x, y, βb-oβ)
Plots y against x using a blue line with circles at data points.
How do you overlay multiple plots on the same figure?
Use hold on.
Example:
hold on
plot(x, z, βrββ) % Adds a red dashed line
How do you add labels, a title, and a legend to a plot?
xlabel(βxβ)
title(βA couple of linesβ)
legend(βy = sin(x)β, βz = 0.5 + cos(x/2)β)
How do you manually set axis limits in MATLAB?
Use the axis function.
Example:
axis([0 10 5 15])
Sets x-axis from 0 to 10 and y-axis from 5 to 15.
How do you load a .mat file and check its contents?
load lesson8
whos data
whos lists variables and their properties.