Working With Variables
-Last updated on 2023-11-13 | +
Last updated on 2023-11-14 | Edit this page
@@ -552,6 +552,19 @@OUTPUT<
If you look at the workspace, you’ll notice that the icon next to each variable is different, and if you hover over it, it will tell you the type of variable it is.
+You can also check the “class” of the variable with the
+class
function:
MATLAB +
+>> class(patient_name)
+OUTPUT +
+ans =
+ 'string'
+OUTPUT<
We can also check if two variables (or even operations) are the same
-MATLAB
->> c3 = frac == mass/age
+>> c3 = frac == mass/age
OUTPUT
@@ -576,10 +589,10 @@ OUTPUT<
We can also combine comparisons. For example, we can check whether frac is smaller than 10 and the age is greater than 5
-MATLAB
->> c4 = frac < 10 && age > 5
+>> c4 = frac < 10 && age > 5
OUTPUT
@@ -593,10 +606,10 @@ OUTPUT<
If we want a “yes” as long as at least one of the conditions are met, we would ask if frac is smaller than 10 or the age is greater than 5
-MATLAB
->> c5 = frac < 10 || age > 5
+>> c5 = frac < 10 || age > 5
OUTPUT
@@ -671,10 +684,10 @@ Arrays<
groups of variables called arrays, or matrices.
We can create an array using square brackets and separating each value with a comma:
-MATLAB
->> A = [1, 2, 3]
+>> A = [1, 2, 3]
OUTPUT
@@ -687,10 +700,10 @@ OUTPUT<
row and 3
columns.
We can create matrices using semi-colons to separate rows:
-MATLAB
->> B = [1, 2; 3, 4; 5, 6]
+>> B = [1, 2; 3, 4; 5, 6]
OUTPUT
@@ -704,10 +717,10 @@ OUTPUT<
the 3x2
we get from the workspace.
3x2
we get from the workspace.
We can also create arrays of other types of data. For example, we could create an array of names:
-MATLAB
->> Names = ["John", "Abigail", "Bertrand", "Lucile"]
+>> Names = ["John", "Abigail", "Bertrand", "Lucile"]
OUTPUT
@@ -717,10 +730,10 @@ OUTPUT<
"John" "Abigail" "Bertrand" "Lucile"
We can use logical values too:
-MATLAB
->> C = [true; false; false; true]
+>> C = [true; false; false; true]
OUTPUT
@@ -741,10 +754,10 @@ OUTPUT<
interesting programming language.
We can, for example, check the whole matrix B and look for values greater than, say, 3.
-MATLAB
->> B > 3
+>> B > 3
OUTPUT
@@ -777,10 +790,10 @@ Suppressing the output
+
MATLAB
->> x = 33;
+>> x = 33;
At first glance nothing appears to have happened, but the workspace
shows the new value was assigned.
@@ -797,10 +810,10 @@ Printing a variable’s value
If we really want to print the variable, then we can simply type its
name and hit Enter,
-
+
MATLAB
->> patient_name
+>> patient_name
OUTPUT
@@ -819,10 +832,10 @@ OUTPUT<
function, in particular, takes just one input – the variable that you
want to print – and what it does is to print the variable in a nice way.
For the variable patient_name, we would use it like this:
-
+
MATLAB
->> disp(patient_name)
+>> disp(patient_name)
OUTPUT
@@ -846,10 +859,10 @@ Keeping things tidy
+
MATLAB
->> clear alive_on_day_3
+>> clear alive_on_day_3
You might be able to see it disappear from the workspace. If you now
try to use alive_on_day_3, matlab will give an error.
@@ -858,10 +871,10 @@ MATLAB<
careful though, there’s no way back!
Another thing you might want to clear every once in a while is the
output pane. To do that, we use the command clc
.
-
+
MATLAB
->> clc
+>> clc
Again, be careful usig this command, there is no way back!
@@ -953,7 +966,7 @@ Keypoints
Arrays
- Last updated on 2023-11-13 |
+
Last updated on 2023-11-14 |
Edit this page
@@ -578,7 +578,7 @@
Show me the solution
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -757,7 +757,7 @@
Show me the solution
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -792,8 +792,8 @@ Slicing character arraysMATLAB
>> element = 'oxygen';
->> disp(['first three characters: ', element(1:3)])
->> disp(['last three characters: ', element(4:6)])
+>> disp("first three characters: " + element(1:3))
+>> disp("last three characters: " + element(4:6))
OUTPUT
@@ -822,7 +822,7 @@
Show me the solution
-
+
- Exercises using slicing
To select all elements from 3rd to last we can use start our
@@ -955,7 +955,7 @@
Keypoints
-
MATLAB
->> x = 33;
+>> x = 33;
At first glance nothing appears to have happened, but the workspace shows the new value was assigned.
@@ -797,10 +810,10 @@Printing a variable’s value
If we really want to print the variable, then we can simply type its
name and hit Enter,
-
+
MATLAB
->> patient_name
+>> patient_name
OUTPUT
@@ -819,10 +832,10 @@ OUTPUT<
function, in particular, takes just one input – the variable that you
want to print – and what it does is to print the variable in a nice way.
For the variable patient_name, we would use it like this:
-
+
MATLAB
->> disp(patient_name)
+>> disp(patient_name)
OUTPUT
@@ -846,10 +859,10 @@ Keeping things tidy
+
MATLAB
->> clear alive_on_day_3
+>> clear alive_on_day_3
You might be able to see it disappear from the workspace. If you now
try to use alive_on_day_3, matlab will give an error.
@@ -858,10 +871,10 @@ MATLAB<
careful though, there’s no way back!
Another thing you might want to clear every once in a while is the
output pane. To do that, we use the command clc
.
-
+
MATLAB
->> clc
+>> clc
Again, be careful usig this command, there is no way back!
@@ -953,7 +966,7 @@ Keypoints
Arrays
- Last updated on 2023-11-13 |
+
Last updated on 2023-11-14 |
Edit this page
@@ -578,7 +578,7 @@
Show me the solution
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -757,7 +757,7 @@
Show me the solution
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -792,8 +792,8 @@ Slicing character arraysMATLAB
>> element = 'oxygen';
->> disp(['first three characters: ', element(1:3)])
->> disp(['last three characters: ', element(4:6)])
+>> disp("first three characters: " + element(1:3))
+>> disp("last three characters: " + element(4:6))
OUTPUT
@@ -822,7 +822,7 @@
Show me the solution
-
+
- Exercises using slicing
To select all elements from 3rd to last we can use start our
@@ -955,7 +955,7 @@
Keypoints
-
MATLAB
->> patient_name
+>> patient_name
OUTPUT
@@ -819,10 +832,10 @@ OUTPUT<
function, in particular, takes just one input – the variable that you
want to print – and what it does is to print the variable in a nice way.
For the variable patient_name, we would use it like this:
-
+
MATLAB
->> disp(patient_name)
+>> disp(patient_name)
OUTPUT
@@ -846,10 +859,10 @@ Keeping things tidy
+
MATLAB
->> clear alive_on_day_3
+>> clear alive_on_day_3
You might be able to see it disappear from the workspace. If you now
try to use alive_on_day_3, matlab will give an error.
@@ -858,10 +871,10 @@ MATLAB<
careful though, there’s no way back!
Another thing you might want to clear every once in a while is the
output pane. To do that, we use the command clc
.
-
+
MATLAB
->> clc
+>> clc
Again, be careful usig this command, there is no way back!
@@ -953,7 +966,7 @@ Keypoints
Arrays
- Last updated on 2023-11-13 |
+
Last updated on 2023-11-14 |
Edit this page
@@ -578,7 +578,7 @@
Show me the solution
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -757,7 +757,7 @@
Show me the solution
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -792,8 +792,8 @@ Slicing character arraysMATLAB
>> element = 'oxygen';
->> disp(['first three characters: ', element(1:3)])
->> disp(['last three characters: ', element(4:6)])
+>> disp("first three characters: " + element(1:3))
+>> disp("last three characters: " + element(4:6))
OUTPUT
@@ -822,7 +822,7 @@
Show me the solution
-
+
- Exercises using slicing
To select all elements from 3rd to last we can use start our
@@ -955,7 +955,7 @@
Keypoints
-
MATLAB
->> disp(patient_name)
+>> disp(patient_name)
OUTPUT
@@ -846,10 +859,10 @@ Keeping things tidy
+
MATLAB
->> clear alive_on_day_3
+>> clear alive_on_day_3
You might be able to see it disappear from the workspace. If you now
try to use alive_on_day_3, matlab will give an error.
@@ -858,10 +871,10 @@ MATLAB<
careful though, there’s no way back!
Another thing you might want to clear every once in a while is the
output pane. To do that, we use the command clc
.
-
+
MATLAB
->> clc
+>> clc
Again, be careful usig this command, there is no way back!
@@ -953,7 +966,7 @@ Keypoints
MATLAB
->> clear alive_on_day_3
+>> clear alive_on_day_3
You might be able to see it disappear from the workspace. If you now try to use alive_on_day_3, matlab will give an error.
@@ -858,10 +871,10 @@MATLAB< careful though, there’s no way back!
Another thing you might want to clear every once in a while is the
output pane. To do that, we use the command clc
.
MATLAB
->> clc
+>> clc
Again, be careful usig this command, there is no way back!
Keypoints
Arrays
-Last updated on 2023-11-13 | +
Last updated on 2023-11-14 | Edit this page
@@ -578,7 +578,7 @@Show me the solution
-We need to select every other element in both dimensions. To do that, we define the apropriate intervals with an increment of 2:
@@ -757,7 +757,7 @@Show me the solution
-We need to tart with row 2
, and subsequently select
every third row:
Slicing character arraysMATLAB
>> element = 'oxygen';
->> disp(['first three characters: ', element(1:3)])
->> disp(['last three characters: ', element(4:6)])
+>> disp("first three characters: " + element(1:3))
+>> disp("last three characters: " + element(4:6))
OUTPUT
@@ -822,7 +822,7 @@
Show me the solution
-
+
- Exercises using slicing
To select all elements from 3rd to last we can use start our
@@ -955,7 +955,7 @@
Keypoints
-
- Exercises using slicing
To select all elements from 3rd to last we can use start our @@ -955,7 +955,7 @@
Keypoints -