How to extract specific values from a table

Dear All I'm aiming to measure the distance between all points in one frame and all points in the next frame. So I want to Create a table (let’s call this “table i”)which only contains points in frame i (where “i" is the index of that frame) Create another table (“table i+1”), which only contains points in frame i+1 Create a 2D distance matrix (width equal to the number of points in frame i and height equal to the number of points in frame i+1) where each element is the Pythagoras distance between two points. How can I implement this as a code ? Thank you

7 Commenti

This question is an extension of a previous question:
@karishma , the first part of your question was already answered in the link above.
I recommend not breaking up your table into multiple tables. Instead, use indexing to identify all points in frame i and another index variable to identify all points in frame i-1. Then you just need to use those two index variables to send the (x,y) coordinates into the distance formula we discussed in your previous question.
karishma koshy
karishma koshy il 15 Lug 2019
Modificato: karishma koshy il 15 Lug 2019
Dear Sir,
I'm not familiar with coding. Can you please help me with a working code of what you are expecting.
I'm trying to get a 2D matrix as it's needed in the later stage. How can I get that.
@karishma, have you tried my answer below?
karishma koshy
karishma koshy il 17 Lug 2019
Modificato: karishma koshy il 17 Lug 2019
Dear Sir, Sorry for the late reply. Thank you so much for the help. I tried it and it's what I want. But I also want to check the distance of coordinate in frame i to all coordinates in i+1, maybe with a nested loop. Can you guide me through.
There are 30 coordinates in each frame. Let's call them c1,c2, c3....c30. Do you want the distance between c1 in frame 1 and [c1:c30] in frame 2? And do you want those distances for all points and between all 1000 frames?
I ask these questions because I'm unsure how you want to organize the data.
For example, do you want a [30 x 30 x 1000] array named A where A(i,j,k) is the distance between xy(i) and xy(j) from frame (k-1) to frame (k)?
This should be well defined before moving forward.
Also, you'll use pdist() or pdist2().
karishma koshy
karishma koshy il 17 Lug 2019
Modificato: karishma koshy il 17 Lug 2019
Yes I want the distance between c1 in frame 1 to c1:c30 in frame 2. Then I want that differences that to be assigned in the form of a 2D distance matrix (width equal to the number of points in frame i and height equal to the number of points in frame i+1) where each element is the Pythagoras distance between two points. I want to how I can use nested for loop for this.
Right now I only need frame 1 and frame 2 alone.
Thank you so much in advance for your help.
Following my example in my answer, here's how to calculate the distance between point 'p' in frame 'f' to all points in frame 'f+1'.
The example shows distances between the 3rd point of frame 4 and all points in frame 5.
f = 4; %frame number 1:1000
p = 3; %point number (within frame) 1:30
% Find distance between point p of frame f and all points in frame f+1
fIdx = T.Frame == f; %index for frame f
f2Idx = T.Frame == f+1; %index for frame f+1
pIdx = max(find(fIdx,p)); %index of point p in frame f
d = pdist2([T.centreX(pIdx),T.centreY(pIdx)],[T.centreX(f2Idx),T.centreY(f2Idx)])';
Note that d(p) should be 0 since the (x,y) coordinates do not change between frames.

Accedi per commentare.

 Risposta accettata

Adam Danz
Adam Danz il 15 Lug 2019
Modificato: Adam Danz il 16 Lug 2019
If I understand your question correctly, you want to measure the distance each (x,y) coordinate travels between frames. Is that correct? It looks like the (x,y) centers of your circles do not change between frames. For example, the first (x,y) coordinate at frame 1 is (144.09, 131,56) and this coordinate is the same for frame 2, 3, etc... When I run the code below, the distance is always 0. Maybe I'm not understanding your problem.
%% Import data and store in table "T"
opts = spreadsheetImportOptions("NumVariables", 4);
opts.Sheet = "in";
opts.DataRange = "A2:D30001";
opts.VariableNames = ["centreX", "centreY", "Radius", "Frame"];
opts.SelectedVariableNames = ["centreX", "centreY", "Radius", "Frame"];
opts.VariableTypes = ["double", "double", "double", "double"];
T = readtable("C:\Users\adanz\Documents\MATLAB\savehere\matlabCentralDocs_trash\DataTable_SER.xlsx", opts, "UseExcel", false);
%% Calculate distance between frames
nFrames = max(T.Frame);
distFcn = @(x1,x2,y1,y2)sqrt((x2-x1).^2 + (y2-y1).^2); %distance function
dist = nan(sum(T.Frame == 1), nFrames-1); %assumes there are an equal number of frames for all frames
for i = 2:nFrames
% Index for frame i
idx2 = T.Frame == i;
% Index for frame i-1
idx1 = T.Frame == i-1;
% Calculate distance
dist(:,i-1) = distFcn(T.centreX(idx2),T.centreX(idx1),T.centreY(idx2),T.centreY(idx1));
end

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by