When ever I have to append to a vector I am doing this.
A = [2 3 4]
A = [A; 3 4 5]
I was wondering if there are any inbuilt functions for this or more elegant ways of doing this in Octave.
The builtin functions are cat, vertcat, and horzcat, found on pages 380-381 of the Octave documentation (v 3.8). They are essentially equivalent to what you have though.
octave:5> A = [2 3 4];
octave:6> A = [A; 3 4 5]
A =
2 3 4
3 4 5
octave:7> B = [4 5 6];
octave:8> B = vertcat(B,[5 6 7])
B =
4 5 6
5 6 7
Another (again equivalent) way would be to directly use matrix indexing (see page 132)
octave:9> C = [6 7 8];
octave:10> C(end+1,:) = [7 8 9]
C =
6 7 8
7 8 9
I think that the most efficient is to use this built in function that you have posted in the question(I'm relying on other experts in octave i did not check it completely; The standard is that matrix operations are generally faster than iterative ones, I don't know what the inner mechanism that allows this to be enabled yet).
Because a vector is a type of matrice, this solution will work for concatinating vectors (of any type) too:
vector = [vector ; value]
Related
In Matlab, given matrix 'a' as:
a = [1 2 3 4 5 6 7 8 9]';
but a few million rows deep, what would be the best way to convert it to the format shown in 'b'?
b = [1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9]
where each row in 'b' contains N consecutive values from 'a'?
I can write a function but was wondering if there's a faster, more built-in way?
Another option is the built-in Hankel matrix, for example in your case:
hankel(1:4,4:9)
or in the general case for a vector a and block size N
hankel(a(1:N),a(N:numel(a)))
I have no idea if the implementation of hankel is better or worse than #Luis Mendo solution, so test and see which solution is better for you...
Let
a = [1 2 3 4 5 6 7 8 9].'; % data
N = 4; % block size
If you have the Image Processing Toolbox: use im2col with the 'sliding' option:
b = im2col(a(:), [N 1], 'sliding');
Or, without any toolbox: use implict expansion (or bsxfun) to create the appropriate indexing matrix:
b = a((1:numel(a)-N+1)+(0:N-1).');
I am having difficulty concatenating vectors in MATLAB.
A = [1
2
3]
B = [6
7
8
9
10]
Desired result:
C = [1
2
3
6
7
8
9
10]
where the sizes of A and B are different in every iteration of my script and I want to form the concatenated resulting vector, C, which has a dynamic size.
This is what I have tried:
A = [1
2
3];
B = [6
7
8
9
10];
Vertical concatenation of two vectors/matrices is what you want, done like this...
C = [A; B];
... or this...
C = [A
B];
... or this...
C = vertcat(A,B);
All three of these give
C = [1
2
3
6
7
8
9
10]
% As you requested...
You were running into trouble because you were trying to use horzcat
C = horzcat(A',B');
Horizontal concatenation merges matrices horizontally, i.e.
C = [1, 6
2, 7
3, 8
?, 9
?, 10]
So to avoid this, you've transposed the matrices to make them rows instead of columns, then transposed the result back?? You just need vertcat! I have shown the shorthand and full form for this above.
Try:
A = [1 2 3];
B = [4 5 6 7 8 9 10];
C = [A B]
For vertical vectors A' and B' use:
C = [A;B]
The fool-proof way is this:
C = [A(:);B(:)];
If you use this method then it does not matter if A and B are row vectors, column vectors, or even matrices.
I want to make left cyclic permutation using MATLAB.
Consider matrix p :
p = [2 3 4 5];
Output :
[2 3 4 5;
3 4 5 2;
4 5 2 3;
5 2 3 4];
I hope the code is available for bigger data. Anyone please help me to make this in code using MATLAB.
A loop free alternative:
[X, Y] = meshgrid(1:numel(p));
p(mod(X+Y-2,numel(p))+1)
This is one approach:
cell2mat(arrayfun(#(n) circshift(p,[0 -n]),0:3,'uni',0).')
ans =
2 3 4 5
3 4 5 2
4 5 2 3
5 2 3 4
Note that arrayfun is really just a loop disguised as a one-liner. Thus explicitly writing out a loop to do the same thing might be equally fast/slow.
This question already has answers here:
Got confused with a vector indexed by a matrix, in Matlab
(2 answers)
Closed 8 years ago.
Suppose:
a =
1 2 3
4 5 6
2 3 4
and
b =
1 3 2
6 4 8
In MATLABa(b) gives:
>> a(b)
ans =
1 2 4
3 2 6
What is the reason for this output?
when you have a matrix a:
a =
1 2 3
4 5 6
7 8 9
and b:
b =
1 3 4
3 2 6
then a(b) is a way of adressing items in a and gives you:
>> a(b)
ans =
1 7 2
7 4 8
to understand this you have to think of a als a single column vector
>> a(:)
ans =
1
4
7
2
5
8
3
6
9
now the first row of b (1 3 4) addresses elements in this vector so the first, the 3rd and the forth element of that single column vector which are 1 7 and 2 are adressed. Next the secound row of b is used as adresses for a secound line in the output so the 3rd, the 2nd and the 6th elements are taken from a, those are 7 4 and 8.
It's just a kind of matrix indexing.
Matrix indexes numeration in 'a' matrix is:
1 4 7
2 5 8
3 6 9
This is a possible duplicate to this post where I gave an answer: Got confused with a vector indexed by a matrix, in Matlab
However, I would like to duplicate my answer here as I think it is informative.
That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. A(1), A(2) and so on).
For example, let's say we had a vector a = [1 2 3 4]. Let's also say we had b as a matrix such that it was b = [1 2 3; 1 2 3; 1 2 3]. By doing a(b) to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and you are creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well.
Bear in mind that when you're doing this for a matrix, you access the elements in column major format. For example, supposing we had this matrix:
A = [1 2
3 4
5 6
7 8]
A(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So A(5) would be 2, A(6) would be 4 and so on.
Here are some examples to further your understanding. Let's define a matrix A such that:
A = [5 1 3
7 8 0
4 6 2]
Here is some MATLAB code to strengthen your understanding for this kind of indexing:
A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
B = [1 2 3 4];
C = A(B); % C should give [5 7 4 1]
D = [5 6 7; 1 2 3; 4 5 6];
E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
F = [9 8; 7 6; 1 2];
G = A(F); % G should give [2 0; 3 6; 5 7]
As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.
In order to be complete, let's do this for a vector:
V = [-1 9 7 3 0 5]; % A 6 x 1 vector
B = [1 2 3 4];
C = V(B); % C should give [-1 9 7 3]
D = [1 3 5 2];
E = V(D); % E should give [-1 7 0 9]
F = [1 2; 4 5; 6 3];
G = V(F); % G should give [-1 9; 3 0; 5 7]
NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.
Suppose I have this matrix:
m=[1 2 3;4 5 6; 7 8 9]
I want this matrix in MATLAB:
a=[1,1,1,2,2,2,2,3,3,3;1,1,1,2,2,2,3,3,3;4,4,4,5,5,5,6,6,6;4,4,4,5,5,5,6,6,6;7,7,7,8,8,8,9,9,9;7,7,7,8,8,8,9,9,9]
Please help to make this matrix,
Thanks in advance,
I like the kronecker tensor product
m=[1 2 3;4 5 6; 7 8 9];
a = kron(m,ones(2,3))
each element in a is a product between the current element in m and the matrix in the second argument (which would be clear if the second argument contains other values than 1). But this would do for your example
This may be faster than using a Kronecker product:
m = [1 2 3;4 5 6; 7 8 9]; %// data
rep = [2 3]; %// number of repetitions of rows and columns respectively
a = m(ceil((1:size(m,1)*rep(1))/rep(1)), ceil((1:size(m,2)*rep(2))/rep(2)));