Sorting the vectors in matlab - matlab

I have three vectors in matlab:
x=[2, 3, 2, 3, 3]
y=[1, 5, 1, 5, 5]
Q=[7, 8, 4, 6, 8]
The modified vectors should be
x=[2, 3]
y=[1, 5]
Q=[12, 22 ]
Here x,y represents coordinates and Q a value depending upon (x,y).
The coordinates are getting repeated, like (2,1) has come twice, then I need to modify the vectors x and y representing unique coordinates and summing the values of Q for the particular coordinates e.g. (2,1) has come twice and the values of Q at those coordinates are 7 and 5 then for modified vectors coordinate (2,1) has to come once and corresponding Q value 7+5=12 and similarly for (3,5) Q is 8+6+8=22.

[XY, ~, ic] = unique([x' y'],'rows')
xu = XY(:,1).'; % The unique x you want
yu = XY(:,2).'; % The unique y you want
Qu = accumarray(ic,Q').';

Related

Subscript to linear index

M is a matrix of prime numbers from 1 to 23 (in row order)
I don't understand how the second code line replaces the diagonal entries of M with 1. I am also confused because the function sub2ind converts row/column subscript to linear index both of which has nothing to do with the value of the entry.
M = zeros(3); M(:) = primes(23); M = M'
M(sub2ind(size(M), 1:3, 1:3)) = 1
sub2ind(size(M), 1:3, 1:3) returns an array [1, 5, 9] of the linear index of entries: (1,1), (2,2), (3,3). In M(sub2ind(size(M), 1:3, 1:3)) you are accessing M as a vector, it is equivalent to writing M([1, 5, 9]) so you are accessing the matrix with linear indexing, and you are assigning the value 1 to those entries: M(sub2ind(size(M), 1:3, 1:3)) = 1 so that changes the value of those entries

Matrix with sliding window elements

I have time series, and I applying some user defined function to every W elements in time series.
Right now I am just using for loop, slide window of size W an apply my function to elements in a window at every iteration.
I am using Matlab and it is very inefficient with a "for loops" so I would love to vectorize this operation.
As a solution I see transforming signal with length N to a matrix with size (N - 1, W) where each row is time series in different windows and applying function to this matrix.
So, my questions are:
How to transform my initial time series to such a matrix?
Let's say I am sliding window with step X. So that not (N - 1, W) matrix will appear, but ((N - 1) / X, W). (Every Xth row of the matrix in [1])
Example:
Let's say my time series is:
T = [1, 5, 6, 8, 10, 14, 22]
W = 3
X = 1
=> I would love to get
[[1, 5, 6],
[5, 6, 8],
[6, 8, 10],
[8, 10, 14],
[10, 14, 22]]
If
W = 3
X = 2
=> I would love to get
[[1, 5, 6],
[6, 8, 10],
[10, 14, 22]]
Creating the right indices with bsxfun should most certainly help:
ind = bsxfun(#plus, 1:W, (0:X:numel(T)-W).');
out = T(ind);
Creating the right indices is the first step, delineated by the first line of code. What this code does is that it creates a 2D matrix where each row are the elements to access per window of interest. If you want to gain intuition on how the code generates the indices, look specifically at the first case where X = 1; and W = 3;.
We can see that the first row consists of accessing elements 1, 2, 3. The second row consists of accessing elements 2, 3, 4... up until the last row, which is 5, 6, 7. We can see that we have to access neighbouring elements in a window, and so the base indices need to go from 1, 2, 3, or in general from 1 to W. We now need to offset these indices so that they are centred at the right elements in T per window. The offset for the first window is simply 0, the next offset for the second window is simply 1 up until the last row which is 3. We see that for each row, we add 1 more to the base indices as the rows increase. Therefore, we add 1 to each base index for the second row, then 2 for each base index in the third row and so on. If you add the base indices with the offset indices, you thus finally get the correct indices to access the right elements in T.
Similarly if X = 2; and W = 3;, we see that we still have base indices of 1, 2, 3. However, the right elements to access now are 1, 2, 3 for the first row, then 3, 4, 5 for the second row then 5, 6, 7 for the third row. For each row, we now offset the base indices by 2 instead of 1 now. Therefore the second row we add 2 to each base index, then we add 4 to each base index for the third row and so on.
In general, the base indices are created using a vector 1:W and the offset indices are created using a vector 0:X:numel(T)-W. The subtraction of W is required so that we don't go out of bounds when sampling the signal as per the requirement. To create these indices that we just talked about, bsxfun handles this for us.
We create a row vector of 1:W which corresponds to the base indices and a column vector of (0:X:numel(T)-W).' which corresponds to the offsets per window. Note that the first offset starts at 0, then we increment by X amount to ensure that the correct centre is calculated to place our base indices at. We stop until we hit numel(T)-W elements, which is the condition you have stated. By using bsxfun, two temporary 2D matrices are created where the row vector is duplicated for as many rows as there are rows as there are in the column vector and the column vector is duplicated for as many columns as there are in the row vector. Once you add these two matrices together, you get the resulting index matrix.
Running the code with W = 3; and X = 1; gives:
>> T = [1, 5, 6, 8, 10, 14, 22];
>> X = 1;
>> W = 3;
>> ind = bsxfun(#plus, 1:W, (0:X:numel(T)-W).')
ind =
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
Similarly if W = 3; and X = 2; we also get:
>> T = [1, 5, 6, 8, 10, 14, 22];
>> X = 2;
>> W = 3;
>> ind = bsxfun(#plus, 1:W, (0:X:numel(T)-W).')
ind =
1 2 3
3 4 5
5 6 7
You can verify for yourself that these indices correspond to the correct elements in T to create your desired matrix in this case.
We finally use this to index into our matrix to grab the right elements:
out = T(ind);
Doing this for X = 1; and W = 3; gives:
>> out = T(ind)
out =
1 5 6
5 6 8
6 8 10
8 10 14
10 14 22
Similarly for X = 2; and W = 3; gives:
>> out = T(ind)
out =
1 5 6
6 8 10
10 14 22
Based on rayryeng's answer I wrote a function that does exactly this, plus some additional functionality. It's designed for generating indices for autoregression on a univariate time series. It can easily be used for the multivariate case by simply using the same indices and concatenating the referenced data.
It returns the indices to the predictor variable X (as per your request) and for the regressors y as well. In addition you have the option of applying a 'mask' to the predictor variables X while sliding the window. For example, with a window of 21 steps, you can select [T-2 T-3 T-5 T-8 T-13 T-21] for X and T for y
You can also change the prediction horizon - how many steps into the future the indices for y are. For example X = [T-1 T-2] and y = T+2
Hopefully someone else will find this useful.
% get_Sliding_Indexes:
% Useful for autoregression on a univariate time series.
% Returns the indexes for the predictor and response variables
% according to a sliding window.
%
% Copyright (C) 20016 Florin Schimbinschi
%
% Parameters:
% numRecords - the number of records in the dataset
%
% windowLag - number of past samples to take - it will be equal to
% the size of the predictor vector X. Default 10
%
% predHorizon - the prediction horizon is the number of steps into
% the future that predictions are to be made. Default 1
%
% windowPattern - by default the window will take all consecutive
% values in the past over the window lag size, however it is
% possible to sample using a custom pattern.
% For example taking every second value can be done by setting
% this parameter to 1:2:5. Default 1:windowLag
%
% stepSize - number of steps taken when window is moved. Default 1
%
% Returns:
% indX - predictor variable indexes
% indY - response variable indexes
%
%
% windowPattern = 1:2:9 __ structure between [] is moved to
% / \ / the right by stepSize units
% >------[(9-7-5-3-1)---(y)]--------------->
% \_______/ \_/
% X = [13579] predHorizon = 3
%
%
% Example on a multivariate time series (two) with 6 records:
%
% data2d = [ .1 .2 .3 .4 .5 .6
% .11 .22 .33 .44 .55 .66]';
%
% [X, y] = getSlidingIndexes(size(data2d,1), 4)
% X =
% 1 2 3 4
% 2 3 4 5
% y =
% 5
% 6
%
% Assuming we are interested in the second series (column):
%
% series2 = data2d(:,2);
%
% series2(X)
% ans =
% 0.1100 0.2200 0.3300 0.4400
% 0.2100 0.3300 0.4400 0.5500
%
% series2(y)
% ans =
% 0.5500
% 0.6600
%
function [indX, indY] = get_Sliding_Indexes(numRecords, ...
windowLag, predHorizon, windowPattern, stepSize)
if ~exist('numRecords','var') || isempty(numRecords)
error('The number of records in the dataset is not specified');
end
if ~exist('stepSize','var') || isempty(stepSize)
stepSize = 1; % steps taken when moving the window
end
if ~exist('predHorizon','var') || isempty(predHorizon)
predHorizon = 1; % aiming to predict this many steps in the future
end
if ~exist('windowLag','var') || isempty(windowLag)
windowLag = 10; % number of time steps to look back
end
if exist('windowLag','var') && (windowLag > numRecords)
error('The size of the window is larger than the number of observations');
end
if ~exist('windowPattern','var') || isempty(windowPattern)
windowPattern = 1:windowLag; % pattern of sampling data
end
if exist('windowPattern','var') && windowPattern(end) > windowLag
error('The window pattern must stop at the window lag specified');
end
% the number of samples in the window
maxSample = max(windowPattern);
indX = bsxfun(#plus, windowPattern, ...
(0:stepSize:(numRecords - maxSample - predHorizon))');
indY = bsxfun(#plus, max(windowPattern) + predHorizon, ...
(0:stepSize:(numRecords - maxSample - predHorizon))');
end
You can also find the code here: https://au.mathworks.com/matlabcentral/fileexchange/58730-get-sliding-indexes-numrecords--windowlag--predhorizon--windowpattern--stepsize-

Data Plot in Matlab

I have two arrays say X and Y with same dimension. I can plot each points (x,y) by plot(X,Y). But how can I color them according to their given labels?
Say X = [3, 4, 2, 5, 6], Y = [2, 2, 1, 5, 6] and label = [1, 2, 2, [1,2], 2]. Here I all have to do is to color points with label=1 with blue and points in label=2 by red. How can I do this?
There are several ways to optimize this code and even get away without using the loop but this should get you started
for i=1:length(X)
xdot=X(i)
ydot=Y(i)
Ldot=label(i)
col=[1 0 0;0 0 1];
plot(xdot,ydot,'color',col(Ldot,:),'marker','o');
hold on
end

pick random numbers in certain range from vector

how would you use randperm to randomly pick three numbers out of a range of 5 in a vector?
i have a vector like this:
A = [1 2 3 4 5 6 7 8 9 10]
now from every 5 consecutive values i want to randomly pick 3 of them:
A_result = [1 3 5 6 7 9]
any help is appreciated!
This one uses different random indices in every 5-group.
A = [1 2 3 4 5 6 7 8 9 10]
B = reshape(A,5,[]) % (5 x 2)
ind = cell2mat(arrayfun(#(x)sort(randperm(5,3))',1:size(B,2),'UniformOutput',false)) % (3 x 2), row indices into B
ind = bsxfun(#plus,ind,size(B,1)*(0:size(B,2)-1)) % (3 x 2), linear indices into B
C = B(ind) % (3 x 2) result
C(:)' % result vector
Every sort(randperm(5,3))' call generates a random column vector with 3 ascending numbers from 1 to 5, like [1;3;4] or [2;4;5]. arrayfun with the dummy argument x calls this 2 times in this example, because A consists of 2 sub-vectors of length 5. With the argument 'Uniform output' set to false, it generates a cell array of these random vectors, and cell2mat converts it to the (3 x 2)-matrix ind. The bsxfun call converts the values in the matrix ind to a matrix of linear indices into matrix B or A.
For your (900 x 25)-matrix, do
A = rand(900,25); % (900 x 25)
B = A'; % (25 x 900)
ind = cell2mat(arrayfun(#(x)sort(randperm(25,17))',1:size(B,2),'UniformOutput',false)); % (17 x 900), row indices into B
ind = bsxfun(#plus,ind,size(B,1)*(0:size(B,2)-1)); % (17 x 900), linear indices into B
C = B(ind); % (17 x 900), result
You can use reshape and randsample
rA = reshape( A, 5, [] ); % groups of 5 in a a row
A_result = rA( randsample( 5, 3, false ), : );
A_result = reshape( A_result, 1, [] );
You can pre-generate all possible picking patterns, and then randomly select one such pattern for each group. This approach is suitable for small group sizes, otherwise it may use a lot of memory.
A = 1:10; %// example data
n = 5; %// group size. Assumed to divide numel(A)
k = 3; %// how many to pick from each group
S = numel(A);
patterns = nchoosek(1:n, k); %// all possible picking patterns
K = size(patterns, 1);
p = randi(K, S/n, 1); %// generate random indices for patterns
pick = bsxfun(#plus, patterns(p,:).', (0:n:S-1)); %'// to linear indices
A_result = A(pick(:));

extracting vertices of a triangle which is in y-coordinate matrix format

I'm using the "fuzarith" function for division of two fuzzy numbers;
C = fuzarith(X, A, B, operator)
x is a row vector that defines the x coordinates of the points at which the operation(in this case a division) is carried out. I think the mathematical term is abcissa.
A and B are triangular fuzzy numbers defined by their 3 vertices using the function trimf(x,[a b c]), where a,b and c are the x-coordinates of the vertices as shown below:
for
x=0:0.1:10; and
P=trimf(x,[3 6 8]);
Now if I use the division operator, let's say I do [3 6 8]/[1 2 3],
the result,r, is mathematically [1 3 8] but matlab gives me an <101x1> column vector of the ordinates of the plot of r v/s x.
The question is how to extract the 3 vertices from the column vector.
For this or similar problem you can do:
y = fuzarith(x, A, B, 'div');
result = x([find(y ~= 0, 1, 'first') - 1, find(y == 1), find(y ~= 0, 1, 'last') + 1]);