X by Y matrix in RDBMS - rdbms

How do I represent an X by Y matrix in a table in an RDBMS? Imagine if I have to exhaustively record all the distances between postal codes in a country, X axis would be "From" and the Y axis would be "To", all intersections where the postal code of From == To will be 0, of course.

A table with postal codes (if you need to store any extra information, for example the city) and a table with 3 columns:
postal code 1 id | postal code 2 id | distance between them

Related

MATLAB: 2D DATA TO 3D ! How to?

I have a 34200 x 4 table. This table shows the 30 years monthly amount of pr (precipitation) in some latitudes and longitudes. So the columns are lat, lon, date, and pr. I want to convert it to a 3D matrix that longitude x latitude x month.
I attach my table.
Please tell me how to do it I'm a beginner. If I don't want to convert it based on the month I could but this issue is so complicated for me.
Please look at my table it's only 235 KB I upload it to my DropBox so please click on Open in the top right side and click download.
Here is my image
Inspecting your data, your latitude and logitude values actually represent 95 unique locations which are scattered seemingly randomly. You can see that in the figure below.
length(unique(C.lat)) % 95
length(unique(C.lon)) % 95
scatter(C.lat, C.lon)
If the locations were spaced in a grid, it would make sense to use lat and lon as the axes of a data matrix. But instead, it is better to use only one axis representing the unique locations. This then leaves you with a second axis representing the date.
length(unique(C.date)) % 360
360 * 95 % 34200 - the number of values we have
Reformatting the data
Therefore, I would store the data in a 2D matrix as follows.
locations_lat = unique(C.lat, 'stable');
locations_lon = unique(C.lon, 'stable');
dates = unique(C.date, 'stable');
data = reshape(C.pr, length(dates), length(locations_lat)); % size 360 x 95
Then, to check that this has worked, choose a random example.
location_num = 27;
date_num = 242;
lat = locations_lat(location_num) % 14.68055556
lon = locations_lon(location_num) % 65.23111111
date = dates(date_num) % 2/1/2009
precipitation = data(date_num, location_num) % 16.7179
Searching for that position and date in the original tale, we have:
9602| 14.6805555600000 65.2311111100000 '2/1/2009' 16.7179000000000
If A is your data with the 4 columns [34200x4] then you can create a 3-dimensional matrix like this:
B = zeros(len(A),3)
B(:, 1) = A(:, 1)
B(:, 2) = A(:, 2)
B(:, 3) = A(:, 3)
Possibly even:
B(:,1:3) = A(:, 1:3)
Depending how your data is set, you may need to transpose which is:
B = 'B
You can map data over as long as the dimensions match.
You can implement a for loop if you have even more columns or for dynamic data entries.

Auto-correlation of each column of a matrix in matlab

My data is included in a matrix (dim: 900 x 10) called input_data_matrix, each column of this matrix has 900 time-series random signals (light readings integer values).
I want to compute the relation (or correlation) between these 900 readings of same column independently, (not correlation with the other columns readings), such that I can get 10 correlation result values corresponding to the 10 column which are indicate how much the 900 readings of each column are correlate,
So, my question is how I can compute this in matlab and what is the best type of correlation to do this.
If I have understood correctly, what you want is the autocorrelation of each column of your input data. In that case, I would use the xcorr function (https://es.mathworks.com/help/signal/ref/xcorr.html), which for a given vector computes its autocorrelation. The code would be the following:
[m, n] = size(input_data_matrix);
output_matrix = zeros(m, n);
for i = 1:n
output_matrix(:,i) = xcorr(input_data_matrix(:,i));
end

How to sort the rows of a multidimensional matrix in Matlab without changing the order of the elements in the first column?

I have a 3D matrix A(i, j, k). The problem is the following:
I have a number of rooms. I use the first dimension (the i's) to denote the room IDs. Each room has a number of chairs in it. I use the 2nd dimension (the j's) to denote the chairs' IDs. Each chair has coordinates x,y,z. I use the 3rd dimension (the k's) to denote the coordinates.
For example, A(4,3,1) denotes the 4th room, 3rd chair, x-coordinate; A(4,3,2) denotes the same room and chair but the y-coordinate; and A(4,3,3) the z-coordinate.
I need to sort the chairs in each room independently of the other rooms, according to one of the dimensions.
Let's say I want to sort the chairs of the first room only, that is A(1 , : , :), according to their x-coordinate, that is A(1 , : , 1).
Could anyone help me on how to do that in Matlab 2016b?
Thanks a lot!
I think this does what you want:
A = randi(99,3,3,3); % example data
room = 1; % desired room
coord = 1; % desired coordinate
[~, ind] = sort(A(room,:,coord)); % get indices of the sorting
B = A; % result. Initiallize
B(room,:,:) = B(room,ind,:); % apply sorting to chairs in that room

3D-plot values at specific coords

I have a 6000*3 matrix Points of 6000 (x,y,z) points representing a surface in space.
I also have a 6000*3 matrix fieldValues such that each row in Points is the location of the field value in the same row in fieldValues:
Points(1,:) = -0.0198 -0.0889 -0.0561
fieldValues(1,:) = 0.7184 -0.1626 -0.0184
so in (-0.0198 -0.0889 -0.0561) which is a point in space, I have a field which its x part is 0.7184, y part is -0.1626 and z part is -0.0184.
How can I plot fieldValues as a function of Points so I can observe how the field acts according to where it is.
Thanks

Interpolation with MATLAB in EMG processing

I have 3 EMG recordings for 2 muscles, with a sampling rate of 1000Hz. In other words I have 3 matrices of EMG data; each has 2 rows (for 2 muscles).
However the number of samples (columns) in each isn't the same: the first one has 2600 samples, the second has 2500 samples and the third one has 2550 samples.
I want to make their lengths the same as each other, to get 3 matrices with the same number of rows and columns. I think it is foolish to cut the bigger ones and use just 2500 columns. However if I want to do so, I don't know whether I should cut from the start or end of them?
Is there a way in MATLAB to interpolate the data to get 3 matrices, each of size 3 x 2600?
All 3 matrices belong to the same movement, and I want to match the samples.
You most likely want to look at using interp1 in this situation. This performs an interpolation between your points so that you can sample at any position on the x-axis.
http://www.mathworks.com/help/matlab/ref/interp1.html
I have the following example which has some random sample data sample1, sample2 and sample3. These variables are of lengths 2600, 2500 and 2550 respectively.
sample1 = exp(2*linspace(0,1,2600)+rand(1, 2600));
sample2 = exp(linspace(0,1,2500)+rand(1, 2500));
sample3 = exp(3*linspace(0,1,2550)+rand(1, 2550));
I have a desired length (I am using a length which corresponds to your shortest sample size)
desiredlength = 2500;
You can then interpolate your data with the following code (note the default is a linear interpolation):
adjusted = zeros(3, desiredlength);
adjusted(1, :) = interp1(linspace(0,1,length(sample1)), sample1, linspace(0,1,desiredlength));
adjusted(2, :) = interp1(linspace(0,1,length(sample2)), sample2, linspace(0,1,desiredlength));
adjusted(3, :) = interp1(linspace(0,1,length(sample3)), sample3, linspace(0,1,desiredlength));
plot(adjusted')
linspace(a, b, n) is a function which gives you a vector of n points between a and b, and for sample1 I am transforming from linspace(0, 1, 2600) to linspace(0, 1, 2500)
I hope this helps.