How to obtain values from a table in matlab
1 回表示 (過去 30 日間)
古いコメントを表示
I have a table with two columns as circle's centre position and third one with frame number 1-1000. I want to obtain the values of circle centres in frame n and n-1 at a time, inorder to make matrix out of it
How can I do that?
0 件のコメント
採用された回答
Adam Danz
2019 年 7 月 14 日
編集済み: Adam Danz
2019 年 7 月 14 日
I'm not sure what you mean by "in order to make a matrix out of it". If this demo doesn't address the problem, please provide an example. Otherwise, here's how to extract the (x,y) coordinates for a given frame.
%Fake data
T = table((1:9)',(2:10)',[1;1;1;2;2;2;3;3;3],'VariableNames',{'X','Y','Frame'});
f = 2; % frame number
frameIdx = T.Frame == f; %row numbers of frame n
frameIdx = T.Frame == f-1; %row numbers of frame n-1 (not used in this demo)
% FROM HERE YOU CAN DO LOTS OF THINGS LIKE...
% ...isolate rows of table that belong to frame n
T(frameIdx,:)
% ...isolate only the (X,Y) values that belong to frame n
T(frameIdx,{'X','Y'})
% ...put (X,Y) values from frame n into a matrix
T{frameIdx,{'X','Y'}}
I recommend keeping the data in the table rather than extracting it into a matrix unless your analysis requres a matrix.
5 件のコメント
Adam Danz
2019 年 7 月 15 日
The loop will probably look something like this; T is your table.
% Create fake data
T = table((1:9)',(2:10)',[1;1;1;2;2;2;3;3;3],'VariableNames',{'X','Y','Frame'});
n = size(T,1)'; %number of rows in your table
distFcn = @(x1,x2,y1,y2)sqrt((x2-x1)^2 + (y2-y1)^2); %distance function
% Loop through each row starting at row #2
d = zeros(n-1,1); %create a vector where the distances will be stored
for i = 2:n
d(i-1) = distFcn(T.X(i-1),T.X(i),T.Y(i-1),T.Y(i));
end
And you can do that without a loop, too.
distFcn = @(x1,x2,y1,y2)sqrt((x2-x1).^2 + (y2-y1).^2); %distance function
% ^ ^ note the dots
d = distFcn(T.X(2:end),T.X(1:end-1),T.Y(2:end),T.Y(1:end-1))
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Other Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!