Accessing and assigning matrix elements
Accessing and assigning matrix elements.
FDTD STACK MODE DGTD CHARGE HEAT FEEM INTERCONNECT
Command |
Description |
---|---|
x = [u; v; w] |
Create a column vector. u,v,w can either be scalars or matrices of compatible dimension. |
x = [u, v, w] |
Create a row vector. u,v,w can either be scalars or matrices of compatible dimension. |
x(7) = 5; |
Set the 7th element of x to 5. |
x(7) = y(2); |
Set the 7th element of x to the 2nd element of y. |
x(3,1,8) = 3; |
Set an element of a multidimensional matrix to 3. The script will check that indices are withing range of the corresponding dimensions. |
x(2:5,1) = 1:4; |
Set a sub-matrix of x to values 1:4. In the assignment A(I,...) = B, if B is not a scalar, the sub-matrix A(I,...) must be the same same size as B. If B is a scalar, then all the values of the sub-matrix are set to B. |
x(2:5,1) = 1; |
Set all the values in a sub-matrix of x to 1. |
x = y(1:10,2,1:20); |
x is equal to a sub-matrix of y. |
x = matrix(2,3); x(4)=7; |
Multi-dimension matrices can be accessed with a single index. |
x=y(z); |
Indices stored in matrix (z) used to select elements of matrix y. length(x) will equal length(z). |
Example
This example shows how to access a multi dimensional matrix with a single index.
x=matrix(2,3); x(1,1)=1; x(2)=2; # same as x(2,1)=2; x(3)=3; # same as x(1,2)=3; x(6)=6; # same as x(2,3)=6; ?x; result: 1 3 0 2 0 6
This example shows how to access data from one matrix with indices stored in another matrix. This is very useful with commands such as findpeaks.
y=10:20; # create a vector of integers 10-20 z=2:2:6; # selected values (2,4,6) ?x=y(z); # x will equal the 2nd, 4th, and 6th value of y. result: 11 13 15
This example shows how to create matrices with the semicolon and colon operators.
?C=[1,2,3;4,5,6]; result: 1 2 3 4 5 6 ?A=matrix(3,1); result: 0 0 0 ?B=matrix(3,4)+1; result: 1 1 1 1 1 1 1 1 1 1 1 1 ?C=[A,B]; result: 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1
See Also