Use neural network to predict coordinates - coordinates

I have a dataset that has some values from different sources for a particular coordinate XY.
How can I transform these coordinates so I can train a neural network to predict the coordinates X and Y from similar data inputs?
I have 12 inputs in the network but how should I transform the output?
My coordinates range from 0 to 63 max so should I use 6 bits for X and 6 bits for Y so the output has 12 bits?
[0,0,0,0,0,0,0,0,0,0,0,0]
| X | | Y |

Related

uniform quantizer for sine wave with amplitude -1 db

i have a signal sine wave with an amplitude of -1 db
The quantizer step Δ is 1/7. The middle quantization level is 0 and the other levels are ±kΔ,, k=1-7. the output is 4-bit unsigned integer which values from 0 to 14 corresponding to the 15 quantization levels.
how i can calculate the 4 bit output

Ranking two vectors of numbers relative to each other in MATLAB

I have two set of numbers and want to compare and rank them relative to each other in MATLAB.
The data is:
x = [3 7 8 25 33 52 64 65 78 79 91 93];
y = [7 10 12 27 30 33 57 62 80 83 85 90];
I started with the for/if/else commands and got stuck in the middle.
In other words, I want to get the answer through MATLAB how many times the numbers in the first group (x) are bigger than the ones in the second group (y).
I got started with sorting etc.
n1 = length(data1);
n2 = length(data2);
xs = sort(x);
ys = sort(y);
r1 = zeros(1,n1);
r2 = zeros(1,n2);
I am open to use other commands than this kind of sorting and for/if/else, it doesn't really matter, just need some help in the right direction.
I am not entirely sure I understand what you're trying to do there. Is it safe to assume that the two vectors will be of the same size?
You could simply do an element wise division of the 2 sorted vectors and get the statistics from there.
For example:
div = xs./ys;
max_div = max(div)
mean_div = mean(div)
This is equivalent to running a for loop and dividing each element in the xs array by each element in the ys array for that corresponding index. The 'max' and 'mean' are obviously the largest quotient and the mean quotient.
In MATLAB, to find how many times each of the numbers in vector x are bigger than numbers in vector y:
sum(x > y.')
This uses the transpose of y to create a size(x) by size(y) matrix with a 1 when a number in x is greater than a number in y, then sums each column.
For your data, the result is the following vector, with an item for each number in x:
[0 0 1 3 5 6 8 8 8 8 12 12]
The vectors x and y don't have to be sorted. If you need the total number of times, just apply sum again to the result.

Avoiding sub2ind and ind2sub

I need to access several indices around a certain point in 3D.
For example, for point (x1,y1,z1) I need to get all the indices of its 3x3x3 neighborhood such that (x1,y1,z1) is centered. For neighborhood of size 3, I do it with
[x,y,z] = meshgrid(-1:1,-1:1,-1:1);
x_neighbors = bsxfun(#plus,x,x1);
y_neighbors = bsxfun(#plus,y,y1);
z_neighbors = bsxfun(#plus,z,z1);
Here, I center x1,y1,z1 to (0,0,0) by adding the distances from (x1,y1,z1) to any point in the 3x3x3 box.
that gives me the coordinates of (x1,y1,z1) 3x3x3 neighborhood. I then need to turn them into linear indices so I can access them:
lin_ind = sub2ind(size(volume),y_neighbors,x_neighbors,z_neighbors);
that is costly in what I do.
My question is, how to avoid sub2ind. If inx is the linear index of (x1,y1,z1),
inx = sub2ind(size(volume),y1,x1,z1);
how can I find the 3x3x3 neighborhood of the linear index by adding or subtracting or any other simple operation of inx?
As long as you know the dimensions of your 3D array, you can compute the linear offsets of all the elements of the 3x3x3 neighborhood. To illustrate this, consider a 2D example of a 4x5 matrix. The linear indices look like this:
1 5 9 13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20
The 3x3 neighborhood of 10 is [5 6 7 9 10 11 13 14 15]. The 3x3 neighborhood of 15 is [10 11 12 14 15 16 18 19 20]. If we subtract off the index of the central element, in both cases we get [-5 -4 -3 -1 0 1 3 4 5]. More generally, for MxN matrix we will have [-M-1 -M -M+1 -1 0 1 M-1 M M+1], or [(-M+[-1 0 1]) -1 0 1 (M+[-1 0 1])].
Generalizing to three dimensions, if the array is MxNxP, the linear index offsets from the central element will be [(-M*N+[-M-1 -M -M+1 -1 0 1 M-1 M M+1]) [-M-1 -M -M+1 -1 0 1 M-1 M M+1] (M*N+[-M-1 -M -M+1 -1 0 1 M-1 M M+1])]. You can reshape this to 3x3x3 if you wish.
Note that this sort of indexing doesn't deal well with edges; if you want to find the neighbors of an element on the edge of the array you should probably pad the array on all sides first (thereby changing M, N, and P).
Just adding the (generalized) code to #nhowe answer:
This is an example for neighborhood of size 5X5X5, therefore r (the radius) is 2:
ns = 5;
r = 2;
[M,N,D] = size(vol);
rs = (1:ns)-(r+1);
% 2d generic coordinates:
neigh2d = bsxfun(#plus, M*rs,rs');
% 3d generic coordinates:
pages = (M*N)*rs;
pages = reshape(pages,1,1,length(pages));
neigh3d = bsxfun(#plus,neigh2d,pages);
to get any neighborhood of any linear index of vol, just add the linear index to neigh3d:
new_neigh = bxsfun(#plus,neigh3d, lin_index);

Random selection of matrix columns

I have an m x n matrix and I want to use it in some neural networks applications in MATLAB.
For example,
A = [ 24 22 35 40 30 ; 32 42 47 45 39 ; 14 1 10 5 9 ; 2 8 4 1 8] ;
I want to randomly train some columns and test the other remaining columns.
So, the first matrix will contain three random, distinct columns taken from the original matrix A, while the second matrix contains the remaining two columns.
How can I extract these matrices ?
This will do:
s = randperm(5);
train = A(:, s(1:3));
test = A(:, s(4:end));
Neural Network Toolbox comes with a set of functions that do this for you, such as dividerand and divideblock.

Physical significance of the rotation of the filter matrix in filter2 function

While using MATLAB 2D filter funcion filter2(B,X) and convolution function conv(X,B,''), I see that the filter2 function is essentially 2D convolution but with a rotation by 180 degrees of the filter coefficients matrix. In terms of the outputs of filter2 and conv2, I see that the below relation holds true:
output matrix of filter2 = each element negated of output of conv2
EDIT: I was incorrect; the above relation does not hold true in general, but I saw it for a few cases. In general, the two output matrices are unrelated, due to the fact that 2 entirely different kernels are obtained in both which are used for convolution.
I understand how 2D convolution is performed. What I want to understand is the implication of this in image processing terms. How do I visualize what is happening here? What does it mean to rotate a filter coefficient matrix by 180 degrees?
I'll start with a very brief discussion of convolution, using the following image from Wikipedia:
As illustrated, convolving two 1-D functions involves reflecting one of them (i.e. the convolution kernel), sliding the two functions over one another, and computing the integral of their product.
When convolving 2-D matrices, the convolution kernel is reflected in both dimensions, and then the sum of the products is computed for every unique overlapping combination with the other matrix. This reflection of the kernel's dimensions is an inherent step of the convolution.
However, when performing filtering we like to think of the filtering matrix as though it were a "stencil" that is directly laid as is (i.e. with no reflections) over the matrix to be filtered. In other words, we want to perform an equivalent operation as a convolution, but without reflecting the dimensions of the filtering matrix. In order to cancel the reflection performed during the convolution, we can therefore add an additional reflection of the dimensions of the filter matrix before the convolution is performed.
Now, for any given 2-D matrix A, you can prove to yourself that flipping both dimensions is equivalent to rotating the matrix 180 degrees by using the functions FLIPDIM and ROT90 in MATLAB:
A = rand(5); %# A 5-by-5 matrix of random values
isequal(flipdim(flipdim(A,1),2),rot90(A,2)) %# Will return 1 (i.e. true)
This is why filter2(f,A) is equivalent to conv2(A,rot90(f,2),'same'). To illustrate further how there are different perceptions of filter matrices versus convolution kernels, we can look at what happens when we apply FILTER2 and CONV2 to the same set of matrices f and A, defined as follows:
>> f = [1 0 0; 0 1 0; 1 0 0] %# A 3-by-3 filter/kernel
f =
1 0 0
0 1 0
1 0 0
>> A = magic(5) %# A 5-by-5 matrix
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Now, when performing B = filter2(f,A); the computation of output element B(2,2) can be visualized by lining up the center element of the filter with A(2,2) and multiplying overlapping elements:
17*1 24*0 1*0 8 15
23*0 5*1 7*0 14 16
4*1 6*0 13*0 20 22
10 12 19 21 3
11 18 25 2 9
Since elements outside the filter matrix are ignored, we can see that the sum of the products will be 17*1 + 4*1 + 5*1 = 26. Notice that here we are simply laying f on top of A like a "stencil", which is how filter matrices are perceived to operate on a matrix.
When we perform B = conv2(A,f,'same');, the computation of output element B(2,2) instead looks like this:
17*0 24*0 1*1 8 15
23*0 5*1 7*0 14 16
4*0 6*0 13*1 20 22
10 12 19 21 3
11 18 25 2 9
and the sum of the products will instead be 5*1 + 1*1 + 13*1 = 19. Notice that when f is taken to be a convolution kernel, we have to flip its dimensions before laying it on top of A.