Meaning of : in the parentheses

  • Thread starter Thread starter jasoncurious
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the use of the colon operator (:) in MATLAB, specifically in the context of matrix manipulation and indexing. Participants explore its meaning and application in a program for partial pivoting.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant inquires about the meaning of the colon operator in MATLAB, seeking clarification on its function within matrix indexing.
  • Another participant explains that the colon operator represents an entire row or column in a matrix, providing examples using the magic square function in Octave, which is similar to MATLAB.
  • Further examples illustrate how m(2,:) selects the entire second row and m(:,2) selects the second column of a matrix.
  • Participants suggest experimenting with different matrix operations to better understand the behavior of the colon operator.

Areas of Agreement / Disagreement

Participants generally agree on the function of the colon operator in MATLAB, with no significant disagreement noted.

Contextual Notes

None noted.

Who May Find This Useful

Individuals learning MATLAB or Octave, particularly those interested in matrix operations and programming for numerical methods.

jasoncurious
Messages
7
Reaction score
0
Hi guys, I am currently doing a Matlab program for partial pivoting. I looked at my friend's example:

Code:
% 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
 
Physics news on Phys.org
The colon stands for a whole line in the matrix. Observe:
Code:
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
(Matlab works the same way...)
 
Last edited by a moderator:
Thanks bro.
 
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.
 

Similar threads

  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 0 ·
Replies
0
Views
2K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K