I have vibration data(g) in x and y direction for a Ball Bearing in 2 columns.Is there a way to find Manhattan distance just with this data & time? - matlab

I have frequency data in x and y direction for a Ball Bearing. And the Absolute time ofcourse in Another column. Is there a way to find Manhattan distance just with Frequecy and Absolute time.. so, can anyone just guide me?
For example the given file is like below
3.54393190998923540E+9 -6.80819749832153320E-2 -1.33635997772216800E-2
3.54393190998923540E+9 -6.80819749832153320E-2 -1.33635997772216800E-2
3.54393190998923540E+9 -6.80819749832153320E-2 -1.33635997772216800E-2
3.54393190998923540E+9 -6.80819749832153320E-2 -1.33635997772216800E-2
Here, first column is Time, And 2nd and 3rd columns are frequency data in x and y. How do we find manhattan distance here?

You can read all of the data in at once using dlmread and then access each of the columns individually:
M = dlmread('datafile.txt');
dlmread will figure out the delimiter and give you the correct number of columns in M:
M =
3.5439e+09 -6.8082e-02 -1.3364e-02
3.5439e+09 -6.8082e-02 -1.3364e-02
3.5439e+09 -6.8082e-02 -1.3364e-02
3.5439e+09 -6.8082e-02 -1.3364e-02
Now you can access column 2, for example, like so:
>> M(:,2)
ans =
-0.068082
-0.068082
-0.068082
-0.068082

Try
perl -i -pe 's/ /,/g;s/^/a$.=/' junk.txt

Related

Matlab trouble with a lot of data. Vibration data is 3 million rows long

I have vibration data(g) in x and y direction for a Ball Bearing in 2 columns. Is there a way to find Manhattan distance just with this data & time?
If all you look for is summing data(:,1) and data(:,2), (as in #Austin answer) using vectored sum will be the fastest:
out = data(:,1)+data(:,2);
Here is an example:
function timming_mat_dist
data = randi(100,1e6,2);
timeit(#()arrmat(data))
timeit(#()vecmat(data))
end
function out = arrmat(data)
man_hat_dist_func = #(x,y)x+y;
out = arrayfun(man_hat_dist_func,data(:,1),data(:,2));
end
function out = vecmat(data)
out = data(:,1)+data(:,2);
end
and the results are 2.8936 seconds for arrmat and 0.0020463 seconds for vecmat.
However, if you want to compute all distances you should use pdist:
out = pdist(data,'cityblock');
but be aware that the output will be huge (and MATLAB probably won't let allocate so much memory for that) as #hammadian pointed in his answer.
It depends on which pair you want to measure the distance between. If you are looking for all possible combination of points, this will be memory and time consuming as the space requirement will be stored in 2D matrix of size:3 million * 3 million /2 ( /2 as distance between a and b is the same as b and a), so the matrix is upper triangular.
This is 3000000*3000000/2=4.5 Tera bytes. This can be improved but still will be very high in memory requirements.
If you are trying to find manhattan distance for each set of x and y, I would recommend using arrayfun
The usage is as follows:
man_hat_dist_func = #(x,y)x+y;
out = arrayfun(man_hat_dist_func,data(:,1),data(:,2));
OR
out = arrayfun(#(x,y)x+y,data(:,1),data(:,2));
This should be your fastest option to solve the distances. You should also consider pre-allocating memory for out using out = zeros(Length(data(:,1)),1);.

how can i obtain polynomial form of a .wav(audio) file in matlab

My teacher has given me a task of expressing a sound file(.wav) in polynomial form in MATLAB(curve fitting),.
I tried using polyfit() like this:-
a=wavread('filename');
x=linspace(0,1,239915);
p=polyfit(x,a,4);
display(p);
but I am getting error as
X and Y vectors must be the same size
Please help me.
Size of a is not 239915, it is for x we have taken these many samples, for line space, by the way, why do we need x for polyfit().
Thank you in advanced
Try this
a = wavread('filename')
x = linspace(0, 1, numel(a))';
p = polyfit(x,a,4);
display(p)
Note that I transpose linspace output as it returns a row vector where wavread returns a column vector, which is why you get the error message.

Matlab - finding nearest neighbor in three-dimensional coordinate system [duplicate]

This question already has an answer here:
Finding K-nearest neighbors and its implementation
(1 answer)
Closed 8 years ago.
Welcome!
I have a set of n matlab matrix with the following structure:
xyz_1 -3,37200000000000 2,80000000000000 5,03400000000000
xyz_2 -2,21700000000000 1,74500000000000 7,45300000000000
.... .................. ................ ................
xyz_n -1,39300000000000 0,00700000000000000 6,35500000000000
where the first column is the name of matrix, while the next three columns are the xyz coordinates. I' am looking for an efficient way to find the nearest neighbor. I would like to give the matrix name and k of potential neighbors as the input parameter then program will find the nearest neighbors giving me the result matrix in the following form:
[nearest_neighbor_name_1; distance_between_quoted_element_and_nearest_neigbor_1
nearest_neighbor_name_2; distance_between_quoted_element_and_nearest_neigbor_2
nearest_neighbor_name_....; distance_between_quoted_element_and_nearest_neigbor_....
nearest_neighbor_name_k; distance_between_quoted_element_and_nearest_neigbor_k]
I tried to use the knnsearchunfortunately without effect. Thank you for your help!
Is the traditional way of unsatisfactory in some way? Evaluate the distance from each point to your test point and then sort the distances...
%define the "k" entries that you are interested in assessing
mydata_xyz = [-3.37200000000000 2.80000000000000 5.03400000000000;
-2.21700000000000 1.74500000000000 7.45300000000000;
<the rest of your data here>
-1.39300000000000 0.00700000000000000 6.35500000000000];
%define the point about which you are looking for the nearest neighbors
mypoint_xyz = [ <whatever your xyz coordinate is];
%compute the distance from the given point to all of the other test points.
%Note the use of the transpose (apostrophe) to ensure that it sums in the
% correct direction
distance = sqrt(sum( ((mydata_xyz - ones(size(mydata_xyz,1),1)*mypoint_xyz).^2)' ));
%sort to get the nearest neighbor followed by the next nearest neighbors
[sorted_distance, Isort] = sort(distance);
%print out each one of the points, from closest to farthest
for I=1:length(Isort)
disp(['Point ' num2str(Isort) ...
', dist = ' num2str(distance(Isort(I))) ...
', xyz = ' num2str(mydata_xyz(Isort(I),:))]);
end

Efficient way for variance of a continuous variable A at a point i? Matlab

I have a continuous variable A=gamrnd(5,0.4,1000,28) and an output variable Y=lognrnd(7,1.9,1000,28)
A
7.6472 3.4284 6.3352 8.0480 8.1021
12.3371 5.1611 6.3986 9.3687 9.5652
8.7700 5.2980 6.0307 2.8651 12.6011
12.2042 4.5636 6.0570 7.1348 8.6586
7.8960 5.5213 3.7105 6.4875 7.4891
Y
1.9733 14.0951 14.0951 14.0951 14.0951
9.4284 11.7573 15.6730 25.4495 24.6680
3.4724 4.4953 7.1237 9.4191 18.4504
8.9548 8.9548 8.9548 8.9548 8.9548
1.4834 2.5393 2.5393 2.5393 2.5393
I'd like to calculate the variance of Y for a specific value of A (or width) of that red box? let's say I split the domain of A into 20 red boxes, I would like to calculate the variance of Y for each box. That is to say:
$Var(Y|A=a_i)$
Any thought how I'd do that?
My thoughts so far:
[i j]=find(9.5<=A & A<=10.5)
sig=var(reshape((Y([i j])),length(i)*2,1))
but this is correct but rather ad-hoc. Let's say I had a hundred divisions in A. Is it possible to use something more efficient?
accumarray to the rescue!
%# split A into 100 chunks
nChunks = 100;
Aidx = round(A/maxA*(nChunks-1))+1;
%# get the number of data points just in case
nDataPoints = histc(Aidx,1:nChunks);
%# calculate the variance
varA = accumarray(Aidx,Y,[100,1],#var,NaN);

MATLAB XYZ to Grid

I have a tab separated XYZ file which contains 3 columns, e.g.
586231.8 2525785.4 15.11
586215.1 2525785.8 14.6
586164.7 2525941 14.58
586199.4 2525857.8 15.22
586219.8 2525731 14.6
586242.2 2525829.2 14.41
Columns 1 and 2 are the X and Y coordinates (in UTM meters) and column 3 is the associated Z value at the point X,Y; e.g. the elevation (z) at a point is given as z(x,y)
I can read in this file using dlmread() to get 3 variables in the workspace, e.g. X = 41322x1 double, but I would like to create a surface of size (m x n) using these variables. How would I go about this?
Following from the comments below, I tried using TriScatteredInterp (see commands below). I keep getting the result shown below (it appears to be getting some of my surface though):
Any ideas what is going on to cause this result? I think the problem lies with themeshgrid command, though I'm not sure where (or why). I am currently putting in the following set of commands to calculate the above figure (my X and Y columns are in meters, and I know my grid size is 8m, hence ti/tj going up in 8s):
F = TriScatteredInterp(x,y,z,'nearest');
ti = ((min(x)):8:(max(x)));
tj = ((min(y)):8:(max(y)));
[qx,qy] = meshgrid(ti,tj);
qz = F(qx,qy);
imagesc(qz) %produces the above figure^
I think you want the griddata function. See Interpolating Scattered Data in MATLAB help.
Griddata and tirscattteredinterp are extremely slow. Use the utm2deg function on the file exchange and from there a combination of both vec2mtx to make a regular grid and then imbedm to fit the data to the grid.
I.E.
for i = 1:length(X)
[Lat,Lon ] = utm2deg(Easting ,Northing ,Zone);
end
[Grid, R] = vec2mtx(Lat, Lon, gridsize);
Grid= imbedm(Lat, Lon,z, Grid, R);
Maybe you are looking for the function "ndgrid(x,y)" or "meshgrid(x,y)"