I have two arrays in matlab representing tracked points by two different methods. In each array the first column contains the frame number, and columns two and three are the x, y coordinates. The tracks don't necessarily start or finish at the same frame, but I want to compare the distances between tracks for any common frames.
My input data is something along the lines of:
d1 = d2 =
[ 130 50 20; [ 128 48 17;
131 50 21; 129 52 19;
... ...
195 70 36 ] 180 65 34 ]
I can find intersecting frame numbers using
commonFrames = intersect(d1(:,1), d2(:,1));
but I'm stuck on how to align these arrays (preferably without a for loop)?
I'd looking for an output along the lines of [frameNumber x1 y1 x2 y2] where x1, y1 are values from frame frameNumber of array d1, and x2, y2 are values from frame frameNumber of array d2.
'intersect' function has two additional output values: indices of common values in input arrays.
Your script can be following:
[commonFrames,ia,ib] = intersect(d1(:,1), d2(:,1));
commonData = [commonFrames d1(ia,2:3) d2(ib,2:3)];
Related
I would like to fit a certain curve. My data is stored in "x" and "y2". To fit that curve, there are predefined functions like "fit".
My example:
x = [20 30 40 50 60 70 80 90 100 200];
y = [4.229 2.514 1.714 1.143 0.8 0.629 0.514 0.4 0.349 0.057];
p_0 = 2*10^(-5);
y2 = 10.^(y/20)*p_0;
[xData,yData] = prepareCurveData(x,y2);
fit_type = 'pchipinterp';
[fitresult] = fit(xData,yData,ft);
The result "fitresult" is now stored in the MATLAB Workspace as a "1x1 cfit" object.
Is there any way to get an actual array out of fitresult with a array length that I can manually define? (e.g. MATLAB Workspace: fitresult: 1x3841 double)
edit ur script:
fit_type = 'pchipinterp';
[fitresult] = fit(xData,yData,fit_type);
fitresult have two classes Coefficients and Shape-preserving (pchip) interpolant, in each one there is one component p and fitresult(x)
fitresult =
Shape-preserving (pchip) interpolant:
fitresult(x) = piecewise polynomial computed from p
Coefficients:
p = coefficient structure
to extract the results add those code lines to ur script (name is up to u):
1. piecewise_polynomial_computed_from_p = fitresult(x)
2. coefficient_structure = fitresult.p
here coefficient_structure is a constructor:
coefficient_structure =
form: 'pp'
breaks: [20 30 40 50 60 70 80 90 100 200]
coefs: [9x4 double]
pieces: 9
order: 4
dim: 1
to extract results from it type for example :
Breaks = coefficient_structure.breaks
results is :
Breaks =
20 30 40 50 60 70 80 90 100 200
I iterate through a 100x100 array and pick every time four neighbours (one left of the center node, one above, one right and one below), like in the picture below
the red one is the center node and the blue ones are the neighbours. I struggle to find a convenient way in MATLAB to pick randomly one of the neighbours.
The following assumes that
Each entry is replaced by one of its original neighbours, independently of what happens to other entries.
Each neighbour has the same probability of being picked.
Neighbourhood is defined cyclically. Thus, for example, in the first column the "left" neighbour belongs to the last column.
The code builds a cyclically extended matrix for convenience, and then uses linear indexing to (randomly) select the neighbours.
x = [10 20 30 40; 50 60 70 80; 90 100 110 120]; % example data
x_ext = x([end 1:end 1], [end 1:end 1]); % cyclically extended matrix
ind = bsxfun(#plus, (2:size(x,1)+1).', (1:size(x,2))*(size(x,1)+2)); % linear indices
% of the original matrix in the extended matrix
delta = [-1 1 -size(x_ext,1) size(x_ext,1)]; % possible displacements for neighbours,
% as linear indices
r = delta(randi(4, size(x))); % generate random displacements
result = x_ext(ind + r); % pick neighbours in extended matrix
Example:
>> x
x =
10 20 30 40
50 60 70 80
90 100 110 120
>> result
result =
20 30 70 30
90 100 60 120
50 110 70 40
I use Matlab to find the best fit line from a scatter plot, but I need to delete some data points. For example I am trying to find the best fit line of
x = [10 70 15 35 55 20 45 30];
y = [40 160 400 90 500 60 110 800];
Now I need to delete all y points that value is over 300, and of course deleting corresponding x points, and then make a scatter plot and find the best fit line. So how to implement this?
Now I need to delete all y points that value is over 300, and of course deleting corresponding x points,
There is standard Matlab trick - Logical Indexing (see for example in matrix-indexing):
x = [10 70 15 35 55 20 45 30]; y = [40 160 400 90 500 60 110 800];
filter = (y<300);
y1 = y(filter);
x1 = x(filter);
plot(x,y,'+b',x1,y1,'or');
You can use polyfit (Matlab Doc) function for linear fit:
ff=polyfit(x1,y1,1);
plot(x,y,'*b',x1,y1,'or',x1,ff(1)*x1 + ff(2),'-g');
grid on;
The best way is to logically filter the dataset, then plot it.
NOTE: Data should be in column format. If it isn't, rotate like x'.
filter = (y<300);
x = x.*filter;
x = [zeros(length(x),1),x]; % this is to get the b(0) coefficient
y = y.*filter;
b = x\y;
x = x(:,2); % cleaning up column of zeros
plot(x,y,'bo')
hold on
plot([min(x),max(x)],(b(1)+b(2))*[min(x),max(x)])
hold off
axis tight
I want to extract the two points (i.e their values) which are marked with black outline in figure. These minima points are 2 and 5. Then after extraction these marked points coordinates I want to calculate the distance between them.
The code that I am using to plot average values of image, calculate minimas and locations is
I1=imread('open.jpg');
I2=rgb2gray(I1);
figure, title('open');
plot(1:size(I2,1), mean(I2,2));
hold on
horizontalAverages = mean(I2 , 2);
plot(1:size(I2,1) , horizontalAverages)
[Minimas locs] = findpeaks(-horizontalAverages)
plot(locs , -1*Minimas , 'r*')
Minima
-86.5647
-80.3647
-81.3588
-106.9882
-77.0765
-77.8235
-92.2353
-106.2235
-115.3118
-98.3706
locs =
30
34
36
50
93
97
110
121
127
136
It is a bit unclear from your question what you are actually looking for, but the following one liner will get you the local minima:
% Some dummy data
x = 1:11;
y = [3 2 1 0.5 1 2 1 0 1 2 3];
min_idx = ([0 sign(diff(y))] == -1) & ([sign(diff(y)) 0] == 1);
figure
plot(x, y);
hold on;
scatter(x(min_idx), y(min_idx))
hold off;
Use the 'findpeaks' function, if you have the signal processing toolbox.
[y,locs]=findpeaks(-x)
will find the local minima. This function has a ton of options to handle all kinds of special cases, so is very useful.
I have a CSV file with X values in the first column, and Y values in the first row, with Z values in the middle like so:
** 39 40 41 42
0.004 2.1802 2.1937 2.2144 2.2379
0.25 1.2409 1.2622 1.2859 1.3073
0.5 1.0538 1.02572 1.04857 1.07059
0.75 0.9479 0.96999 0.98699 1.00675
I can import this into maple as a matrix, but for the maple statistics fit command, it requires the X to be in one column, the Y to be in the second column, and the Z to be in the third column like so:
0.004 39 2.1802
0.004 40 2.1937
0.004 41 2.2144
Is there a way to create the second matrix as Maple wants, or is there a call command for Statistics[Fit] that will allow me to insert the first matrix?
Supposing A is your imported matrix, I create a matrix of three columns X, Y, and Z named XYZ thus:
n:= LinearAlgebra:-RowDimension(A):
m:= LinearAlgebra:-ColumnDimension(A):
XYZ:= Matrix([seq(seq([A[i,1],A[1,j],A[i,j]], j= 2..m), i= 2..n)]):