Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Meaning of : in the parentheses

  1. Dec 7, 2012 #1
    Hi guys, I am currently doing a Matlab program for partial pivoting. I looked at my friend's example:

    Code (Text):
    % Partial Pivoting
    for i=1:n-1
    for j = i+1:n
    if (a(j,i)) > (a(i,i))
    u=a(i,:);
    a(i,:)=a(j,:);
    a(j,:)=u;
    v=b(i,1);
    b(i,1)=b(j,1);
    b(j,1)=v;
    end
    end
    end
    I was wondering what's the meaning of the : in the parentheses. Is it some sort of Matlab keyword? Thanks for helping
     
  2. jcsd
  3. Dec 7, 2012 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    The colon stands for a whole line in the matrix. Observe:
    Code (Text):
    octave:117> m=magic(4)
    m =

       16    2    3   13
        5   11   10    8
        9    7    6   12
        4   14   15    1

    octave:118> m(2,:)
    ans =

        5   11   10    8

    octave:119> m(:,2)
    ans =

        2
       11
        7
       14
     
    ... see?
    ... so m(2,:) says to take the entire second row while the m(:,2) says take the second column.
    http://volga.eng.yale.edu/sohrab/matlab_tutorial.html#selecting_parts_of_vectors_and_matrices__the_colon_operator [Broken]
    (Matlab works the same way...)
     
    Last edited by a moderator: May 6, 2017
  4. Dec 7, 2012 #3
    Thanks bro.
     
  5. Dec 7, 2012 #4

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    No worries ;)
    Generally, you can try out the confusing operations you see on some random matrix and see what happens ... the magic-square function is very useful for this.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Meaning of : in the parentheses
Loading...