VariablesAndVectors

VariablesAndVectors - %Matrix indexing %Guess what you will...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Variables and Vectors %What do the following statements display in Matlab? 1:6 1:2:6 a = [0 1:3:10] b = [8:-2:0] %Multiplication with vectors x = 2:2:10 y = [ 3 2 1 0 -1]; x .* y x * y % gives error x * y' % scalar (single number) sum(x .* y) % same scalar---why? x' * y % 5x5 matrix---why? x = 3:5 y = -1:1 6-x 2*x y*2 y^2 y.^2 y(2) y(2:3) y(6-x) x + y x + y' (x+y)' x .* y x * y x * y' x' * y
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
%Building matrices— %Construct these matrices, which you can do by just cutting and pasting the commands. a = [1 2 3; 4 5 6] b = a' c = ones(3) d = zeros(2,3) e = eye(3) f = diag(1:3) h = [1 1; 2 2]; i = [3 3; 4 4]; j = [5 5; 6 6]; k = [7 7; 8 8]; g = [ h i; j k]
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %Matrix indexing %Guess what you will get before trying these: a(1,1) a(1) % these are strange, until you realize that matlab keeps data by columns a(1,2) a(2,1) a(2) a(1,3) a(3,1) % error here a(3) % this is a(i,j) for what i,j? This is a strange "feature" often accessed by mistake f(1:2,1:2) % submatrices e(1:2,2:3) g(2:3,2:3) diag(g) g(1,:) % colon shortcuts. Note the similarities and differences for transposes g(:,1) a(:,1) b(1,:) a(1,:) b(:,1) a(:,:) a(:) b(:) e(2,3) = 5 %Changing parts of a matrix e(3,2) = 6 e e(1:2,1:2) = h'; e g(2,:) = 1; g g(2,:) = ; %check this one out setting a row to an empty matrix size(g) g...
View Full Document

This note was uploaded on 03/22/2009 for the course COMP 116 taught by Professor Stotts during the Spring '08 term at UNC.

Page1 / 3

VariablesAndVectors - %Matrix indexing %Guess what you will...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online