I want to calculate distances in 3D space. How do I apply my code to all tables in all cells?
2 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Voss
on 10 Jun 2024
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{1} % 1x12 cell array
results_nooutliers{1}{1} % table
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{:})
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
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!