user3263704
user3263704

Reputation: 71

What does it mean in Matlab?

i'm trying to learn Matlab but i don't understand what does this writing means: sauv(:, 2:2:2*N) ? It is written bellow:

subplot(2,2,[1, 2])
plot(sauv(:, 2:2:2*N), sauv(:, 3:2:(2*N+1)),couleur,'LineWidth',2, 'MarkerSize', 2)
grid('on')
hold on
subplot(223)
plot(sauv(:, 1)/Nmax, sauv(:, 2:2:2*N),couleur,'LineWidth',2, 'MarkerSize', 2)
grid('on')
hold on
subplot(224)
plot(sauv(:, 1)/Nmax, sauv(:, 3:2:(2*N+1)),couleur,'LineWidth',2, 'MarkerSize', 2)
grid('on')
hold on

I get that sauv is an empty array which is filled as the calculation ran.

Upvotes: 0

Views: 167

Answers (1)

Dan
Dan

Reputation: 45752

You need to read up on the Matlab colon operator.

If I have a matrix like this:

A = magic(5)

A =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9

And I want to get the whole of column 1 then I can use the : operator to tell Matlab that I want all the rows:

A(:,3)

ans = 

    1
    7
    13
    19
    25

but I can also use it to create a vector with constant increments. So if I want a vector like this [1,2,3,4,5] I can actually just go 1:5. But if I want only every second number then I use two colons: 1:2:5 gives us [1,3,5]. The middle number tells Matlab how much to increment by.

So putting it together A(:, 2:2:5) will choose all the rows by only the even columns because it is the same as A(:, [2,4]) which gives us

ans =

   24    8
    5   14
    6   20
   12   21
   18    2

Upvotes: 1

Related Questions