Scientific Computing & Visualization
Help Contact
About Accounts Computation Visualization Documentation Services

Introduction to MATLAB

Defining matrices via the vector technique

>> A = magic(3)     % define a 3x3 matrix A    

A =

     8     1     6       
     3     5     7      
     4     9     2    

>> b = 1:3     % define b as a 1x3 row vector    

b =         

     1     2     3    

>> [A, b']     % Add b transpose as a 4th column to A    

ans =         

     8     1     6     1       
     3     5     7     2       
     4     9     2     3    

>> [A; b]     % Add b as a 4th row to A    

ans =         

     8     1     6       
     3     5     7       
     4     9     2       
     1     2     3    

>> x = 1:5    

x =         

     1     2     3     4     5      

>> A = 2*ones(3,3)    % yes, there is a zeros m-file too    

A =         

     2     2     2       
     2     2     2       
     2     2     2    

>> y = x';  
>> n = 3;  
>> B = y(:,ones(n,1))  % more general than B = y(:,[1 1 1])  
>>                     % or B=[y y y];    

B =         

     1     1     1       
     2     2     2       
     3     3     3       
     4     4     4       
     5     5     5    
     
>> A = magic(3)    

A =         

     8     1     6       
     3     5     7       
     4     9     2    

>> B = A(:,[1 3 2])      % switch 2nd and third columns of A    

B =         

     8     6     1       
     3     7     5       
     4     2     9    
     
>> A(:,2) = []     % delete second column of A      

A =         

     8     6       
     3     7       
     4     2    

Examples on logical indexing ...

unk

Documentation
Boston University
Boston University
 
OIT | CCS | July 7, 2009  
Scientific Computing & Visualization Boston University home page Boston University home page