I have two matlab vectors. The first has N elements, the other has k*N. I know what k is, and I want to splice the lists such that each element from the first vector appears before the corresponding k elements from the next vector. For example:
k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]
should be combined to look like this:
z = [1 2 3 4 5 6 7 8 9 10 11 12]
Is there an easy way to do this quickly? My x's and y's are pretty big. Thanks!
You can do this via some reshaping
k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]
%# make a k-by-n array
z = reshape(y,k,[]);
%# catenate with x
z = [x;z];
%# reorder
z = z(:)'
Related
I would like to align and count vectors with different time stamps to count the corresponding bins.
Let's assume I have 3 matrix from [N,edges] = histcounts in the following structure. The first row represents the edges, so the bins. The second row represents the values. I would like to sum all values with the same bin.
A = [0 1 2 3 4 5;
5 5 6 7 8 5]
B = [1 2 3 4 5 6;
2 5 7 8 5 4]
C = [2 3 4 5 6 7 8;
1 2 6 7 4 3 2]
Now I want to sum all the same bins. My final result should be:
result = [0 1 2 3 4 5 6 7 8;
5 7 12 16 ...]
I could loop over all numbers, but I would like to have it fast.
You can use accumarray:
H = [A B C].'; %//' Concatenate the histograms and make them column vectors
V = [unique(H(:,1)) accumarray(H(:,1)+1, H(:,2))].'; %//' Find unique values and accumulate
V =
0 1 2 3 4 5 6 7 8
5 7 12 16 22 17 8 3 2
Note: The H(:,1)+1 is to force the bin values to be positive, otherwise MATLAB will complain. We still use the actual bins in the output V. To avoid this, as #Daniel says in the comments, use the third output of unique (See: https://stackoverflow.com/a/27783568/2732801):
H = [A B C].'; %//' stupid syntax highlighting :/
[U, ~, IU] = unique(H(:,1));
V = [U accumarray(IU, H(:,2))].';
If you're only doing it with 3 variables as you've shown then there likely aren't going to be any performance hits with looping it.
But if you are really averse to the looping idea, then you can do it using arrayfun.
rng = 0:8;
output = arrayfun(#(x)sum([A(2,A(1,:) == x), B(2,B(1,:) == x), C(2,C(1,:) == x)]), rng);
output = cat(1, rng, output);
output =
0 1 2 3 4 5 6 7 8
5 7 12 16 22 17 8 3 2
This can be beneficial for particularly large A, B, and C variables as there is no copying of data.
I have a list of strings or arrays with different length or size. I want to use the shortest string and compare with other strings by shifting the shortest string window one by one to do comparison.
Let's say I want to do addition, I have [2 1 3] as my shortest list and want to perform addition on [4 5 7 8 9]
1st addition: [2 1 3] + [4 5 7]
2nd addition: [2 1 3] + [5 7 8]
3rd addition: [2 1 3] + [7 8 9]
How can i do this using matlab?
Thanks
Say A is the longer vector and B the shorter one.
You can use hankel function to create a matrix where each row is a window of length 3 over A
>> hankel(A(1:3),A(3:end))
ans =
4 5 7
5 7 8
7 8 9
Now you just need to call bsxfun to do the desired action on each row:
L=numel(B);
bsxfun(#plus, B, hankel(A(1:L),A(L:end)))
results in
ans =
6 6 10
7 8 11
9 9 12
Where rows contain the desired output vectors.
Note that you can change #plus to #minus or any other user-defined function.
A simpler approach, if you don't care much about speed is using arrayfun and cell2mat. Note that this approach doesn't check which vector is which. a must be shorter than b.
a =
1 2 3
b =
1 3 5 2 4 6
c = cell2mat(arrayfun(#(n) a+b(n:n+numel(a)-1), 1:numel(b)-numel(a)+1,'UniformOutput',0).')
c =
2 5 8
4 7 5
6 4 7
3 6 9
You can create indices of a sliding window using hankel. Example:
a = [2 1 3];
b = [4 5 7 8 9];
idx = hankel(1:numel(a), numel(a):numel(b));
c = bsxfun(#plus, b(idx.'), a);
The result:
>> c
c =
6 6 10 % [2 1 3] + [4 5 7]
7 8 11 % [2 1 3] + [5 7 8]
9 9 12 % [2 1 3] + [7 8 9]
(Note: This assumes b is longer than a, swap them if otherwise).
I think you should do the following, assuming row arrays of doubles:
lenList(1) = length(list1);
lenList(2) = length(list2);
% find minumum length
[minLen, idx] = min(lenList);
% find length difference
lenDiff = abs(diff(lenList));
% initialize result
result = zeros(lenDiff + 1, minLen);
% Check which list is the longest
if idx == 1
shortList = list1;
longList = list2;
else
shortList = list2;
longList = list1;
end
% Perform math
for ii = 1:(lenDiff + 1)
result(ii, :) = shortList + longList(ii:(ii+minLen-1))
end
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.
I'm learning Matlab and I see a line that I don't understand:
A=[x; y']
What does it mean? ' usually means the transponate but I don't know what ; means in the vector. Can you help me?
The [ ] indicates create a matrix.
The ; indicates that the first vector is on the first line, and that the second one is on the second line.
The ' indicates the transponate.
Exemple :
>> x = [1,2,3,4]
x =
1 2 3 4
>> y = [5;6;7;8]
y =
5
6
7
8
>> y'
ans =
5 6 7 8
>> A = [x;y']
A =
1 2 3 4
5 6 7 8
[x y] means horizontal cat of the vectors, while [x;y] means vertical.
For example (Horizontal cat):
x = [1
2
3];
y = [4
5
6];
[x y] = [1 4
2 5
3 6];
(Vertical cat):
x = [1 2 3];
y = [4 5 6];
[x; y] =
[1 2 3;
4 5 6];
Just to be clear, in MATLAB ' is the complex conjugate transpose. If you want the non-conjugating transpose, you should use .'.
It indicates the end of a row when creating a matrix from other matrices.
For example
X = [1 2];
Y = [3,4]';
A = [X; Y']
gives a matrix
A = [ 1 2 ]
[ 3 4 ]
This is called vertical concatenation which basically means forming a matrix in row by row fashion from other matrices (like the example above). And yes you are right about ' indicating the transpose operator. As another example you could use it to create a transposed vector as follows
Y = [1 2 3 4 5];
X = [1; 2; 3; 4; 5];
Y = Y';
Comparing the above you will see that X is now equal to Y. Hope this helps.
Let set the size of x m*n (m rows and n columns) and the size of y n*p.
Then A is the matrix formed by the vertical concatenation of x and the transpose of y (operator '), and its size is (m+p)*n. The horizontal concatenation is done with comma instead of semi-column.
This notation is a nice shorthand for function vertcat.
See http://www.mathworks.fr/help/techdoc/math/f1-84864.html for more information
The semicolon ' ; ' is used to start a new row.
e.g. x=[1 2 3; 4 5 6; 7 8 9] means
x= 1 2 3
4 5 6
7 8 9
So if u take x=[1 2 3; 4 5 6] and y=[7 8 9]'
then z=[x; y'] means
z= 1 2 3
4 5 6
7 8 9
I have two vectors in MATLAB, say:
x = [1 20 3 7 10]
and
y = [2 51 1 9 18]
How can I plot y vs K where x has sorted value order (1 3 7 10 20) with their respective y values like the following?
x = [1 3 7 10 20]
y = [2 1 9 18 51]
Call sort with a second output argument.
x = [1 20 3 7 10]
y = [2 51 1 9 18]
[xsorted, I] = sort(x)
ysorted = y(I)
XY = sortrows([x ; y]');
plot(XY(:,1), XY(:,2));
Concatenate the matrices, transpose them and then you can use sortrows to order by X