ENGR 101 Study Guide - Midterm Guide: Identity Matrix, Standard Deviation, Exponentiation

68 views4 pages
Indexing
Vector Examples
>> m = [2 3 4 5 6 7 8 9]
>> m(1) % gives back the 1st element of vector m, 2
>> m(0) % will give an error
>> max(m) % gives back 9, the largest element
>> m(length(m)) % will give the last element of the vector m (9)
>> m(end) % will also give the last element of the vector m (9)
>> m(:) % gives back the entire vector as a column
>> m(:,:) % gives back the entire vector as a row
>> m(3:5) % gives back the 3rd through 5th elements in a vector
>> m(1:2:end) % gives back the odd-indexed elements in a vector
>> m(1) = 7 % reassigns the 1st element to a value of 7
>> m(3:5) = [-1 -2 -3] % reassigns the 3rd, 4th, and 5th elements
>> m(5) = m(1) % reassigns the 5th element with the 1st element
>> m(5) = [] % deletes the 5th element of the vector
Matrix Examples
>> z = [1 2 3 4 5 ; 6 7 8 9 10] % matrix, 2x5
>> size(z) % gives back [2, 5]
>> sum(z) % gives back [7 9 11 13 15]
>> mean(z) % gives back [3.5 4.5 5.5 6.5 7.5]
>> z(1,1) % accesses the 1st row, 1st column
>> z(2,3) % accesses the 2nd row, 3rd column
>> z(:,3) % accesses the full 3rd column
>> z(2,:) % accesses the full 2nd row
>> z(1:2,3:4) % access the 3rd and 4th elements (columns) of
% the 1st and 2nd rows
>> z([1 2],[3 4]) % another way to write the step above
>> z(2,3) = -3 % reassigns the element at the 2nd row, 3rd column
>> z(1:2,3:4) = [-7 -8; -9 -10] % reassigns the block
>> z(1,:) = [] % deletes the first row
% (note: you can only delete entire rows or columns
% in matrices without getting an error.)
Cell Arrays: Can include matrices as cells
Content Indexing uses {}
Use this form if you want to select and work with the content of a
cell. (This tends not to work well for groups of cells…)
Cell Indexing uses ()
Use this form of indexing if you would like to select the cell itself.
(Or a group of cells.)
>> test{2,1}
ans =
1 2 3
>> test(2,1)
ans =
[1x3 double]
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

% gives back the 1st element of vector m, 2. % will give the last element of the vector m (9) % will also give the last element of the vector m (9) % gives back the entire vector as a column. % gives back the entire vector as a row. % gives back the 3rd through 5th elements in a vector. % gives back the odd-indexed elements in a vector. % reassigns the 1st element to a value of 7. >> m(3:5) = [-1 -2 -3] % reassigns the 3rd, 4th, and 5th elements. % reassigns the 5th element with the 1st element. % deletes the 5th element of the vector. >> z(1:2,3:4) = [-7 -8; -9 -10] % reassigns the block. % (note: you can only delete entire rows or columns. % gives back [7 9 11 13 15] % gives back [3. 5 4. 5 5. 5 6. 5 7. 5] % access the 3rd and 4th elements (columns) of.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers

Related Documents