Time for action - manipulating arrays
1. To delete the second column in
A
, we use:
octave:14> A(:,2) = [] A = 1 3 1 3
2. We can extend an existing array, for example:
octave:15 > b = [b 4 5] b = 1 2 3 4 5
3. Finally, try the following commands:
octave:16> d = [2 4 6 8 10 12 14 16 18 20] d = 2 4 6 8 10 12 14 16 18 20 octave:17> d(1:2:9) ans = 2 6 10 14 18 octave:18> d(3:3:12) = -1 d = 2 4 -1 8 10 -1 14 16 -1 20 0 -1
What just happened?
In Command 14, Octave interprets []
as an empty column vector and column 2 in A is then deleted in the command. Instead of deleting a column, we could have deleted a row, for example as an empty column vector and column 2 in A is then deleted in the command.
octave:14> A(2,:)=[]
On the right-hand side of the equal sign in Command 15, we have constructed a new vector given by [b 4 5]
, that is, if we write out b
, we get [1 2 3 4 5]
since b=[1 2 3]
. Because of the equal sign, we assign the variable b
to this vector and delete the existing value...