I want to calculate distances in 3D space. How do I apply my code to all tables in all cells?

3 次查看(过去 30 天)
Hi,
I want to use the formula d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2) to calculate two distances. Each distance is between two points in 3D space twice. Once from point A to point B, and one is from point A to point C. I have a data set with cell array where each cell contains tables.
he tables in the cells are built up so that the x, y and z positional coordinates of point A are in column 1, 2 and 3. The x, y, and z positional coordinates of point B are in columns 4, 5, and 6. And the x, y, and z positional coordinates of point C are in columns 7, 8 and 9.
I have the code below:
results_distances = cell(size(results_nooutliers));
% Initialize a cell array to store the distances
results_distances = cell(size(results_nooutliers));
% Loop through each cell in the results_no_outliers array
for i = 1:numel(results_nooutliers)
% Get the current table for the participant
this_cell = results_nooutliers{i};
% Check for empty cells
if isempty(this_cell)
continue; % Skip empty cells
end
% Initialize arrays to store distances for right hand and left hand
distances_A_B = zeros(size(this_cell, 1), 1);
distances_A_C = zeros(size(this_cell, 1), 1);
% Calculate distances for each row in the table
for row = 1:size(this_cell, 1)
% Calculate distance for left hand
distances_A_B(row) = sqrt((this_cell{row, 4} - this_cell{row, 1})^2 + ...
(this_cell{row, 5} - this_cell{row, 2})^2 + ...
(this_cell{row, 6} - this_cell{row, 3})^2);
% Calculate distance for right hand
distances_A_C(row) = sqrt((this_cell{row, 7} - this_cell{row, 1})^2 + ...
(this_cell{row, 8} - this_cell{row, 2})^2 + ...
(this_cell{row, 9} - this_cell{row, 3})^2);
end
% Store distances for the current participant in a table
results_distances{i} = table(distances_A_B, distances_A_C, 'VariableNames', {'A_B_Distance', 'A_C_Distance'});
end
When running I get the error:
Undefined function 'minus' for input arguments of type 'table'.
Can anybody tell me what I am doing incorrectly?
I have attached a small sample of my data set (it is much longer in actuality).
Thanks for the help!

采纳的回答

Voss
Voss 2024-6-10
results_nooutliers is not a cell array of tables, but rather a cell array of cell arrays of tables.
load('results_nooutliers.mat')
results_nooutliers % 3x1 cell array
results_nooutliers = 3x1 cell array
{1x12 cell} {1x12 cell} {1x12 cell}
results_nooutliers{1} % 1x12 cell array
ans = 1x12 cell array
Columns 1 through 10 {2155x9 table} {2155x9 table} {2155x9 table} {2154x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} Columns 11 through 12 {2155x9 table} {2155x9 table}
results_nooutliers{1}{1} % table
ans = 2155x9 table
HeadposX HeadPosY HeadPosZ LeftPosX LeftPosY LeftPosZ RightPosX RightPosY RightPosZ ________ ________ ________ ________ _________ ________ _________ _________ _________ 0.9044 -0.74142 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90439 -0.74142 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90439 -0.74143 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.9044 -0.74141 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.9044 -0.7414 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90439 -0.7414 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.9044 -0.74139 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90439 -0.7414 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90439 -0.74139 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90437 -0.74137 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90436 -0.74133 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90434 -0.74133 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90433 -0.74133 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90431 -0.74134 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90433 -0.74132 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816 0.90433 -0.74131 7.2882 1.1839 -0.024158 9.2699 1.0696 -0.75825 7.2816
Since you've written the code to work on a cell array of tables, I'll make results_nooutliers into a 3x12 cell array of tables:
results_nooutliers = vertcat(results_nooutliers{:})
results_nooutliers = 3x12 cell array
Columns 1 through 10 {2155x9 table} {2155x9 table} {2155x9 table} {2154x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} Columns 11 through 12 {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table} {2155x9 table}
And then run the code as you've written it, except I'm going to vectorize the distance calculation (i.e., calculate the distances for all rows at once instead of looping over the rows) and change the name of the variable this_cell, which is a table, to T.
% Initialize a cell array to store the distances
results_distances = cell(size(results_nooutliers));
% Loop through each cell in the results_no_outliers array
for i = 1:numel(results_nooutliers)
% Get the current table for the participant
T = results_nooutliers{i};
% Check for empty tables
if isempty(T)
continue; % Skip empty tables
end
% Calculate distances for left hand
distances_A_B = sqrt( sum( ( T{:,[4 5 6]} - T{:,[1 2 3]} ).^2 , 2) );
% Calculate distances for right hand
distances_A_C = sqrt( sum( ( T{:,[7 8 9]} - T{:,[1 2 3]} ).^2 , 2) );
% Store distances for the current participant in a table
results_distances{i} = table(distances_A_B, distances_A_C, 'VariableNames', {'A_B_Distance', 'A_C_Distance'});
end
results_distances
results_distances = 3x12 cell array
Columns 1 through 10 {2155x2 table} {2155x2 table} {2155x2 table} {2154x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} Columns 11 through 12 {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by