How to separately compute the Euclidean Distance in different dimension? - matlab

I got a question when using pdist, it would be so many thanks if you could give me some advice. The pdist(D) usually gives the sum of the distance for the multiple dimension, however, I want to get the distance separately. For example I have a data set S which is a 10*2 matrix , I am using pdist(S(:,1)) and pdist(S(:,2)) to get the distance separately, but this seems very inefficient when the data has many dimensions. Is there any alternative way to achieve this more efficient? Thanks in advance!

Assuming you just want the absolute difference between the individual dimensions of the points then pdist is overkill. You can use the following simple function
function d = pdist_1d(S)
idx = nchoosek(1:size(S,1),2);
d = abs(S(idx(:,1),:) - S(idx(:,2),:));
end
which returns the absolute pairwise difference between all pairs of rows in S.
In this case
dist = pdist_1d(S)
gives the same result as
dist = cell2mat(arrayfun(#(dim)pdist(S(:,dim))',1:size(S,2),'UniformOutput',false));

Another option, since you're simply taking the absolute difference of the coordinates, is to use bsxfun:
>> D = randi(20, 10, 2) % generate sample data
D =
17 12
14 10
8 4
7 11
19 13
2 18
11 14
5 19
19 12
20 8
From here, we permute the data so that the coordinates (columns) extend into the 3rd dimension and the rows are in the 1st dimension for the 1st argument, and the 2nd dimension for the 2nd argument:
>> dist = bsxfun(#(x,y)abs(x-y), permute(D, [1 3 2]), permute(D, [3 1 2]))
dist =
ans(:,:,1) =
0 3 9 10 2 15 6 12 2 3
3 0 6 7 5 12 3 9 5 6
9 6 0 1 11 6 3 3 11 12
10 7 1 0 12 5 4 2 12 13
2 5 11 12 0 17 8 14 0 1
15 12 6 5 17 0 9 3 17 18
6 3 3 4 8 9 0 6 8 9
12 9 3 2 14 3 6 0 14 15
2 5 11 12 0 17 8 14 0 1
3 6 12 13 1 18 9 15 1 0
ans(:,:,2) =
0 2 8 1 1 6 2 7 0 4
2 0 6 1 3 8 4 9 2 2
8 6 0 7 9 14 10 15 8 4
1 1 7 0 2 7 3 8 1 3
1 3 9 2 0 5 1 6 1 5
6 8 14 7 5 0 4 1 6 10
2 4 10 3 1 4 0 5 2 6
7 9 15 8 6 1 5 0 7 11
0 2 8 1 1 6 2 7 0 4
4 2 4 3 5 10 6 11 4 0
This results in a 3-d symmetric matrix where
dist(p, q, d)
gives you the distance between points p and q in dimension d with
dist(p, q, d) == dist(q, p, d)
If you want the distances between p and q in all (or multiple) dimensions, you should use squeeze to put it in a vector:
>> squeeze(dist(3, 5, :))
ans =
11
9
Note that if you're using MATLAB 2016b or later (or Octave) you can create the same distance matrix without bsxfun:
dist = abs(permute(D, [1 3 2]) - permute(D, [3 1 2]))
The downside to this approach is that it creates the full symmetric matrix so you're generating each distance twice, which could potentially become a memory issue.

Related

MATLAB - generating vector with sequence of values

Given two parameters:
n %number of repetitions per value
k %max value to repeat
I would like to create a vector of size n*k, which is a concatenation of k vectors of size n, such that the i'th vector contains the value i at each coordinate.
Example:
n = 5;
k = 9;
Desired result:
[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9];
Is there an elegant way to achieve this?
Thanks!
quite a few ways to do it:
method 1:
A=1:k
repelem(A',n,1)'
method 2:
A=1:k
kron(A', ones(n,1))'
method 3:
A=1:k
B=repmat(A, n, 1)
B(:)'
method 4:
A=1:k
B=ones(n,1)*A
B(:)'
Here is an alternative method
A = reshape(mtimes((1:k).',ones(1,n)).',1,n*k)
A =
Columns 1 through 22
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5
Columns 23 through 44
5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9
Column 45
9
It multiplies each element by ones n times
>> mtimes((1:k).',ones(1,5)).'
ans =
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
and then reshapes the whole matrix to one vector

Matlab - Finding values within a matrix

How can I find the row that have all the values from A into the matrix B and display the index of the rows using Matlab?
A= [2 5 6];
B=[1 2 4 9 10 15 27 30;
1 2 3 4 5 6 7 8;
1 2 3 5 6 9 22 101;
2 4 5 6 14 20 22 23]
Thanks
With bsxfun in 3D -
ind = find(all(any(bsxfun(#eq,B,permute(A,[1 3 2])),2),3))
With bsxfun again, but keeping it in 2D -
ind = find(sum(reshape(any(bsxfun(#eq,B(:),A(:).'),2),size(B)),2)==numel(A))
With ismember -
ind = find(sum(reshape(ismember(B(:),A(:)),size(B)),2)==numel(A))
With pdist2 from Statistics and Machine Learning Toolbox -
ind = find(sum(reshape(any(pdist2(B(:),A(:))==0,2),size(B)),2)==numel(A))
With knnsearch again from Statistics and Machine Learning Toolbox-
[~,dists] = knnsearch(A(:),B(:))
ind = find(sum(reshape(dists==0,size(B)),2)==numel(A))
Sample run -
A =
2 5 6
B =
1 2 4 9 10 15 27 30
1 2 3 4 5 6 7 8
1 2 3 5 6 9 22 101
2 4 5 6 14 20 22 23
ind =
2
3
4

How to create a random 3D matrix?

Is there any way of creating a 3D matrix randomly? There are ways to create random 2D matrices using randint function. Is there any inbuilt function like that?
E.g. a 4x4 matrix can be generated easily by using the randint function. What if I want to create a matrix of dimension 4x4x3?
You can use randi(imax, size1, size2, size3) function where imax refers to maximum of random integer values (mean upper bound) and 1 is lower bound. You can expand size argument to sizeN what you want.
This is an example of its usage:
>> A = randi(5, 4, 4, 3)
A(:,:,1) =
4 4 5 4
4 1 2 2
2 1 3 3
4 3 2 4
A(:,:,2) =
5 1 5 1
5 2 2 2
3 5 5 4
1 2 2 3
A(:,:,3) =
2 5 2 3
5 2 3 4
3 4 1 5
3 4 1 1
If you read the help carefully, you will notice that the randi function accepts any number of dimensions. You may do randi(10,3,3,3)
randi(10,3,3,3)
ans(:,:,1) =
9 10 3
10 7 6
2 1 10
ans(:,:,2) =
10 10 2
2 5 5
10 9 10
ans(:,:,3) =
8 1 7
10 9 8
7 10 8

how to reshape a cube into one matrix in matlab

I have a cube m by n by k which means i have k matrices m by n. I want to reshape it into one big matrix for example p row and q column (consider each m by n matrix as an element).
How can i do that? Can i use reshape function? for example these matrices when put together form a cube:
1 1 , 2 2 , 3 3 , ... , 16 16
1 1 2 2 3 3 16 16
in the above example, k=16, m=n=2.
i want to reshape them like this:
1 1 5 5 9 9 13 13
1 1 5 5 9 9 13 13
2 2 6 6 10 10 14 14
2 2 6 6 10 10 14 14
3 3 7 7 11 11 15 15
3 3 7 7 11 11 15 15
4 4 8 8 12 12 16 16
4 4 8 8 12 12 16 16
Assuming your input is a n by n by k*k matrix, you can achieve the desired input using:
n=2
k=4
reshape(permute(reshape(M,n,n,k,k),[1,3,2,4]),n*k,n*k);
The inner reshape splits into n by n by k by k, which directly represents the blocks.
The permute swaps the dimensions to let a reshape(...,n*k,n*k) produce the intended result.

Extracting portions of matrix into cell array

I have a pretty large matrix M and I am only interested in a few of the columns. I have a boolean vector V where a value of 1 represents a column that is of interest. Example:
-1 -1 -1 7 7 -1 -1 -1 7 7 7
M = -1 -1 7 7 7 -1 -1 7 7 7 7
-1 -1 7 7 7 -1 -1 -1 7 7 -1
V = 0 0 1 1 1 0 0 1 1 1 1
If multiple adjacent values of V are all 1, then I want the corresponding columns of M to be extracted into another matrix. Here's an example, using the matrices from before.
-1 7 7 -1 7 7 7
M1 = 7 7 7 M2 = 7 7 7 7
7 7 7 -1 7 7 -1
How might I do this efficiently? I would like all these portions of the matrix M to be stored in a cell array, or at least have an efficient way to generate them one after the other. Currently I'm doing this in a while loop and it is not as efficient as I'd like it to be.
(Note that my examples only include the values -1 and 7 just for clarity; this isn't the actual data I use.)
You can utilize the diff function for this, to break your V vector into blocks
% find where block differences exist
diffs = diff(V);
% move start index one value forward, as first value in
% diff represents diff between first and second in original vector
startPoints = find(diffs == 1) + 1;
endPoints = find(diffs == -1);
% if the first block begins with the first element diff won't have
% found start
if V(1) == 1
startPoints = [1 startPoints];
end
% if last block lasts until the end of the array, diff won't have found end
if length(startPoints) > length(endPoints)
endPoints(end+1) = length(V);
end
% subset original matrix into cell array with indices
results = cell(size(startPoints));
for c = 1:length(results)
results{c} = M(:,startPoints(c):endPoints(c));
end
The one thing I'm not sure of is if there's a better way to find the being_indices and end_indices.
Code:
X = [1 2 3 4 5 1 2 3 4 5
6 7 8 9 10 6 7 8 9 10
11 12 13 14 15 11 12 13 14 15
16 17 18 19 20 16 17 18 19 20
1 2 3 4 5 1 2 3 4 5
6 7 8 9 10 6 7 8 9 10
11 12 13 14 15 11 12 13 14 15
16 17 18 19 20 16 17 18 19 20];
V = logical([ 1 1 0 0 1 1 1 0 1 1]);
find_indices = find(V);
begin_indices = [find_indices(1) find_indices(find(diff(find_indices) ~= 1)+1)];
end_indices = [find_indices(find(diff(find_indices) ~= 1)) find_indices(end)];
X_truncated = mat2cell(X(:,V),size(X,1),[end_indices-begin_indices]+1);
X_truncated{:}
Output:
ans =
1 2
6 7
11 12
16 17
1 2
6 7
11 12
16 17
ans =
5 1 2
10 6 7
15 11 12
20 16 17
5 1 2
10 6 7
15 11 12
20 16 17
ans =
4 5
9 10
14 15
19 20
4 5
9 10
14 15
19 20