Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Kodeforståelsesoppgaver

Oppgave 2.1.1: Matriser og vektorer

Hva skrives ut hvis vi kjører det følgende scriptet?

Code Block
titleoppgave_1.m
[1, 2, 3]
[1; 2; 3]
[1, 2, 3; 4, 5, 6]
[1, 2, 3; 4, 5, 6]'
size(1:3)
size((1:3)')

Videoforklaring (8:15)

Widget Connector
urlhttps://www.youtube.com/watch?v=cVha17mZ_KE

Svar

Expand
titleHvis du har prøvd selv, trykk her for å se svaret...
Code Block
ans =
     1     2     3
ans =
     1
     2
     3
ans =
     1     2     3
     4     5     6
ans =
     1     4
     2     5
     3     6
ans =
     1     3
ans =
     3     1

Oppgave 2.1.2: Posisjonsindeksering

Hvilken verdi har M etter at vi kjører dette scriptet?

Code Block
titleoppgave_2.m
M = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12];
M(1,2) = 5;
M(end,end) = 90;
M([1,2], 1) = M([2, 3], 1);
M(:,[3, 2]) = M(:, [2, 3]);
M(1:end,:) = M - 1;

Videoforklaring (10:57)

Widget Connector
urlhttps://www.youtube.com/watch?v=mcobItbGRVo

Svar

Expand
titleHvis du har prøvd selv, trykk her for å se svaret...
Code Block
M =
     4     2     4     3
     8     6     5     7
     8    10     9    89

Oppgave 2.1.3: Logisk indeksering

Hva skrives ut hvis vi kjører det følgende scriptet?

Code Block
titleoppgave_3.m
x = 1:5
x([true,false, true, false, true])
x(true)
x > 3
x(ans)
mod(x,2)
mod(x,2) == 0
x < 2 | x >= 4 & mod(x, 2) == 0
x(x < 2 | x >= 4 & mod(x, 2) == 0)

Videoforklaring (14:44)

Widget Connector
urlhttps://www.youtube.com/watch?v=j7YPR5CcpsY

Svar

Expand
titleHvis du har prøvd selv, trykk her for å se svaret...
Code Block
x =
     1     2     3     4     5
ans =
     1     3     5
ans =
     1
ans =
     0     0     0     1     1
ans =
     4     5
ans =
     1     0     1     0     1
ans =
     0     1     0     1     0
ans =
     1     0     0     1     0
ans =
     1     4

Oppgave 2.1.4: Forskjell på true/false og 0/1

Hva skrives ut hvis vi kjører det følgende scriptet?

Code Block
titleoppgave_4.m
1 + 1
true + true
0 + 0
false + false

x = 1:5
x([true, true, true, true, true])
x([1, 1, 1, 1, 1])
x([false, false, false, false, false])
x([0, 0, 0, 0, 0])

Videoforklaring (7:53)

Widget Connector
urlhttps://www.youtube.com/watch?v=wLjUza6dF60

Svar

Expand
titleHvis du har prøvd selv, trykk her for å se svaret...
Code Block
ans =
     2
ans =
     2
ans =
     0
ans =
     0
x =
     1     2     3     4     5
ans =
     1     2     3     4     5
ans =
     1     1     1     1     1
ans =
   Empty matrix: 1-by-0
Subscript indices must either be real positive integers or logicals.
Error in oppgave_4 (line 10)
x([0, 0, 0, 0, 0]) 

Oppgave 2.1.5: Posisjonsindeksering

Hva skrives ut hvis vi kjører det følgende scriptet?

Code Block
titleoppgave_5.m
alfabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', ...
           'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
disp(alfabet([13, 1, 20, 12, 1, 2, 5, 18, 11, 10, 5, 13, 16, 5, 11, 21, 12, 20]));

Videoforklaring (4:20)

Widget Connector
urlhttps://www.youtube.com/watch?v=pldkXIKA90U

Svar

Expand
titleHvis du har prøvd selv, trykk her for å se svaret...
Code Block
matlaberkjempekult

Oppgave 2.1.6: Matriseoperasjoner

Hva skrives ut hvis vi kjører det følgende scriptet?

Code Block
titleoppgave_6.m
A = [ 1, 2; 3, 4 ];
A .* A
A * A
A ./ A
A / A
A .^ 2
A ^ 2

Videoforklaring (11:02)

Widget Connector
urlhttps://www.youtube.com/watch?v=BeeqqpVOC9c

Svar

Expand
titleHvis du har prøvd selv, trykk her for å se svaret...
 

Kodeoppgaver (total videotid: 1:13:43)

Oppgave 2.2.1: Matrise som datastruktur

...