How to implement an assignment algorithm - matlab

Given a vector y(d x 1) and matrix Z(d x n), I would like to implement the optimal matlab code for the below algorithm.
Initialization:
Let z* be the nearest point from y in Z.
A={z*} (A is the list of assignment for y)
Z = Z \ {z*} (z* point is removed from matrix Z)
do
Let p denotes the center of gravity of A:
Let z* be the nearest center from y in Z(updated Z)
Let q denotes the center of gravity of A U {z*}
if distance(y,q) < distance(y,p) then
A = A U {z*} and Z = Z \ {z*}
else
NOP
endif
while a new assignment is performed
return A(list of assignments)
function A = Assign(x,Z)
%
% x - 1 x N
% Z - k x N
%
OriginalZ = Z;
[~,index]=sort(pdist2(x,Z));
A = index(1);
Z(A,:)=inf;
loop =1;
while(loop)
loop =0;
p=mean(OriginalZ(A,:),1);
[~,index]=sort(pdist2(x,Z));
Ad = [A index(1)];
q = mean(OriginalZ(Ad,:),1);
if pdist([x;q]) < pdist([x;p])
A = Ad; Z(Ad,:)=inf;
loop = 1;
end
end
end

Related

Vectorization of Matrix Quadratics in MATLAB

I am trying to "vectorize" this loop in Matlab for computational efficiency
for t=1:T
j=1;
for m=1:M
for n=1:N
y(t,j) = v{m,n} + data(t,:)*b{m,n} + data(t,:)*f{m,n}*data(t,:)';
j=j+1;
end
end
end
Where v is a (M x N) cell of scalars. b is a (M x N) cell of (K x 1) vectors. f is a (M x N) cell of (K x K) matrices. data is a (T x K) array.
To give an example of what I mean the code I used to vectorize the same loop without the quadratic term is:
B = [reshape(cell2mat(v)',1,N*M);cell2mat(reshape(b'),1,M*N)];
X = [ones(T,1),data];
y = X*B;
Thanks!
For those interested here was the solution I found
f = f';
tMat = blkdiag(f{:})+(blkdiag(f{:}))';
y2BB = [reshape(cell2mat(v)',1,N*M);...
cell2mat(reshape(b',1,M*N));...
reshape(diag(blkdiag(f{:})),K,N*M);...
reshape(tMat((tril(tMat,-1)~=0)),sum(1:K-1),M*N)];
y2YBar = [ones(T,1),data,data.^2];
jj=1;
kk=1;
ll=1;
for k=1:sum(1:K-1)
y2YBar = [y2YBar,data(:,jj).*data(:,kk+jj)];
if kk<(K-ll)
kk=kk+1;
else
kk=1;
jj=jj+1;
ll=ll+1;
end
end
y = y2YBar*y2BB;
Here's the most vectorized form targeted for performance -
% Extract as multi-dim arrays
vA = reshape([v{:}],M,N);
bA = reshape([b{:}],K,M,N);
fA = reshape([f{:}],K,K,M,N);
% Perform : data(t,:)*f{m,n} for all iterations
data_f_mult = reshape(data*reshape(fA,K,[]),T,K,M,N);
% Now there are three parts :
% v{m,n}
% data(t,:)*b{m,n}
% data(t,:)*f{m,n}*data(t,:)';
% Compute those parts one by one
parte1 = vA(:).';
parte2 = data*reshape(bA,[],M*N);
parte3 = zeros(T,M*N);
for t = 1:T
parte3(t,:) = data(t,:)*reshape(data_f_mult(t,:,:),K,[]);
end
% Finally sum those up and to present in desired format permute dims
sums = bsxfun(#plus, parte1, parte2 + parte3);
out = reshape(permute(reshape(sums,T,M,N),[1,3,2]),[],M*N);

Sort an array of points to generate an envelope in MATLAB

I have the next vectors:
A = [0;100;100;2100;100;2000;2100;2100;0;100;2000;2100;0];
B = [0;0;1450;1450;1550;1550;1550;1550;2500;2500;3000;3000;0]
If we plot A and B, we'll obtain the following graphic:
Then, I wonder how to short the points in order to have the next plot:
As you can see, there're some conditions like: all of them form right angles; there's no intersection between lines.
Thanks in advance for any reply!
This can be solved in the traditional recursive 'maze' solution of 'crawling on walls':
%%% In file solveMaze.m
function Out = solveMaze (Pts,Accum)
if isempty (Pts); Out = Accum; return; end % base case
x = Accum(1, end); y = Accum(2, end); % current point under consideration
X = Pts(1,:); Y = Pts(2,:); % remaining points to choose from
% Solve 'maze' by wall-crawling (priority: right, up, left, down)
if find (X > x & Y == y); Ind = find (X > x & Y == y); Ind = Ind(1);
elseif find (X == x & Y > y ); Ind = find (X == x & Y > y ); Ind = Ind(1);
elseif find (X < x & Y == y); Ind = find (X < x & Y == y); Ind = Ind(1);
elseif find (X == x & Y < y ); Ind = find (X == x & Y < y ); Ind = Ind(1);
else error('Incompatible maze');
end
Accum(:,end+1) = Pts(:,Ind); % Add successor to accumulator
Pts(:,Ind) = []; % ... and remove from Pts
Out = solveMaze (Pts, Accum);
end
Call as follows, given A and B as above;
Pts = [A.'; B.']; Pts = unique (Pts.', 'rows').'; % remove duplicates
Out = solveMaze (Pts, Pts(:,1)); % first point as starting point
plot(Out(1,:), Out(2,:),'-o'); % gives expected plot
If all intersections must be at 90 degree angles, we can form a graph of possible connections.
# in pseudo-code, my matlab is very rusty
points = zip(A, B)
graph = zeros(points.length, points.length) # Generate an adjacency matrix
for i in [0, points.length):
for j in [i + 1, points.length):
if points[i].x == points[j].x or points[i].y == points[j].y:
graph[i][j] = graph[j][i] = 1
else
graph[i][j] = graph[j][i] = 0
Note: it may be important/ improve performance to disconnect colinear points.
After this, the solution is reduced to finding a Hamiltonian Cycle. Unfortunately this is an NP-hard problem, but fortunately, a MATLAB solution already exists: https://www.mathworks.com/matlabcentral/fileexchange/51610-hamiltonian-graph--source--destination- (although I haven't tested it).

How to find an arbitrary perpendicular vector of another vector in MATLAB?

I need to create arbitrary perpendicular vector n with components (a, b, c) to another known vector k with components (x,y,z).
The following code creates arbitrary vector n, but I need random numbers for components in the range [-inf, inf] how can I acheive that? (because otherwise vector components created may not exceed some value in given case 10^11 ) Or maybe concept "arbitrary vector" does not require that?
function [a,b,c] = randomOrghogonalVector(x,y,z)
a = 0;
b = 0;
c = 0;
randomDistr = rand * 10^11 * 2 - 10^11; % issue 1
% excluding trivial solution
if x == 0 && y == 0 && z ==0
a = NaN; b = a; c = a;
else
if z ~=0
a = randomDistr;
b = randomDistr;
c = - (x * a + b * y ) / z;
else
if z == 0 && x ~= 0
c = randomDistr;
b = randomDistr;
a = - (z * c + b * y ) / x;
else
if z == 0 && x == 0 && y ~= 0
c = randomDistr;
a = randomDistr;
b = - (z * c + a * x ) / y;
end
end
end
end
The easiest solution I see is to first find a random vector that is orthogonal to your original vector, and then give it a random length. In Matlab, this can be done by defining the following function
function [a, b, c] = orthoVector(x, y, z)
xin = [x;y;z];
e = xin;
while ((e'*xin)==xin'*xin)
e = 2.*rand(3,1)-1;
end
xout = cross(xin, e);
xout = 1.0/(rand()) * xout;
a = xout(1);
b = xout(2);
c = xout(3);
end
Line-by-line, here's what I'm doing:
you asked for this format [a,b,c] = f(x,y,z). I would recommend using function xout = orthoVector(xin), which would make this code even shorter.
Since Matlab handles vectors best as vectors, I'm creating vector xin.
e will be one random vector, different from xin used to compute the orthogonal vector. Since we're dealing with random vectors, we initialize it to be equal to xin.
For this algorithm to work, we need to make sure that e and xin are pointing in different directions. Until this is the case...
...create a new random vector e. Note that rand will give values between 0 and 1. Thus, each component of e will be between -1 and 1.
Ok, if we end, e and xin are pointing in different directions
Our vector xout will be orthogonal to xin and e.
Let's multiply vector xout by a random number between 1 and "very large"
a is first component of xout
b is second component of xout
c is third component of xout
all done.
Optional: if you want to have very large vectors, you could replace line 8 by
xout = exp(1./rand())/(rand()) * xout;
This will give you a very large spread of values.
Hope this helps, cheers!

Generate arguments for scatter3() from volumetric data in Matlab

I used to render my volumetric data with isosurface(), but now I would like to render them as points to accelerate the speed.
The data I have is a 3D array, representing a 3D object. If a voxel belongs this object, then its value is 1, otherwise it is zero.
In order to use scatter3(), I need to generate coordinates for those voxels that have value 1. I currently use the following code to do the job:
function [ x, y, z ] = scatter3_assist( volume )
[R, C, D] = size(volume);
x = zeros( size(volume(:)) );
y = x;
z = x;
idx = 1;
for d=1:D
for r=1:R
for c=1:C
if volume(r, c, d) == 0
x(idx) = 0; y(idx) = 0; z(idx) = 0;
else
x(idx) = C - c + 1; y(idx) = R - r + 1; z(idx) = d;
end
idx = idx + 1;
end
end
end
x(x==0) = [];
y(y==0) = [];
z(z==0) = [];
x = x - 1;
y = y - 1;
z = z - 1;
end
The return value x, y, z are the coordinates of the voxels that belong to my object, and then I called scatter3(x, y, z, '*'); to render it.
Are there any more efficient ways to generate coordinates for specific voxels used by scatter3()?
I would suggest using find to find the linear indices of non-zero entries in your array and then use ind2sub to convert to indices and perform whatever transformation you need to get x, y, and z. Something like:
I = find(volume ~= 0);
[y, x, z] = ind2sub(size(volume),I); %// note that x and y are switched as in your code above
x = size(volume,2)-x;
y = size(volume,1)-y;
You'll want to double check those operations on x and y to make sure they're equivalent to what your code does.

Combining functions with boundaries in matlab

I have a function like this:
f(x) = { x if 0 < x < n
{ n-x if n < x < 2*n
How to enter this function in MATLAB?
Best way is to put this in a sub-function or nested function, or in a separate m-file:
function y = f(x)
n = 4; %// Or whatever your N is
if x <= 0 || x >= 2*n
y = 0;
elseif x < n
y = x;
else
y = n-x;
end
end
or, more generally, when x is a vector/matrix,
function y = f(x)
y = x;
y(x >= n) = n-x(x >= n);
y(x <= 0 | x >= 2*n) = 0;
end
Alternatively, you can of course pass the n as an argument:
function y = f(x, n)
...
end
Alternatively, you can use this anonymous function:
f = #(x) (x>0 & x<n).*x + (x>=n & x<=2*n).*(n-x);
again, optionally, pass the n:
f = #(x,n) ...