Info

# How do I calculate the distance between 3D coordinate points using cell array?

1 view (last 30 days)
lil brain on 7 Feb 2022
Closed: lil brain on 13 Feb 2022
Hi,
I am trying to find the distances between pairs of 3D coordinate points. I have a cell array as my data set. Inside each cell there are 6 columns of cells with 2 rows. Each column of cells represents a dimension (XYZ) and each row of cells represents a participant. The following columns are important here:
position_a = [x1 y1 z1];
position_b = [x2 y2 z2];
x1 = windowed_data{1,1}(:,1);
y1 = windowed_data{1,1}(:,2);
z1 = windowed_data{1,1}(:,3);
x2 = windowed_data{1,1}(:,4);
y2 = windowed_data{1,1}(:,5);
z2 = windowed_data{1,1}(:,6);
I am looking to create a new cell array in the same form of the original windowed_data with all the distances between all the elements in x1,y1,z1 and x2,y,z2 using the following formula:
distance_data = sqrt(sum((position_a - position_b) .^ 2))
As an example, the output I am aiming for should look like this:
distance_sample = distance_data{1,1}{1,1}(:,1);
=
distance_data = sqrt(sum((position_a_sample - position_b_sample) .^ 2))
position_a:sample = [x1_sample y1_sample z1_sample];
position_b_sample = [x2_sample y2_sample z2_sample];
x1_sample = windowed_data{1,1}{1,1}(:,1);
y1_sample = windowed_data{1, 1}{1, 2}(:,1);
z1_sample = windowed_data{1, 1}{1, 3}(:,1);
x2_sample = windowed_data{1,1}{1,4}(:,1);
y2_sample = windowed_data{1, 1}{1, 5}(:,1);
z2_sample = windowed_data{1, 1}{1, 6}(:,1);
How would I go about doing this for the entire data set (each row of cells)? Apologies in advance if some parts of this question are redundant or incorrect. I hope its clear what I am trying to do.
Thanks for the support!
##### 2 CommentsShowHide 1 older comment
lil brain on 8 Feb 2022
I had a look at the code and tried it out but I think that you might have misunderstood the question or perhaps I didnt phase it correctly. I changed the data set slightly to test your code on it.
>> windowed_data_test{1, 1}
ans =
2×6 cell array
{10×1 double} {10×1 double} {10×1 double} {10×1 double} { 10×1 double} { 10×1 double}
{10×2 double} {10×2 double} {10×2 double} {10×2 double} {512×2 double} {512×43 double}
When I run your code on windowed_data_test it gives me an error.
Index in position 2 exceeds array bounds (must not exceed 1).
Error in @(x)norm(x(1,1:3)-x(1,4:6))
Error in @(y)cellfun(@(x)norm(x(1,1:3)-x(1,4:6)),y,'UniformOutput',false)
It seems to me as though your code creates a distance for each cell. Perhaps this example makes it more clear what I am trying to achieve:
position_1a_test = [x1a y1a z1a];
position_2b_test = [x2b y2b z2b];
x1a = windowed_data{1,1}{1,1}(1,1)
y1a = windowed_data{1, 1}{1, 2}(1,1)
z1a = windowed_data{1, 1}{1, 3}(1,1)
x2b = windowed_data{1,1}{1,4}(1,1)
y2b = windowed_data{1, 1}{1, 5}(1,1)
z2b = windowed_data{1, 1}{1, 6}(1,1)
... and if we continue in this way:
position_1n_test = [x1n y1n z1n];
position_2n_test = [x2n y2n z2n];
x1n = windowed_data{1,1}{2,1}(4,2)
y1n = windowed_data{1, 1}{2, 2}(4,2)
z1n = windowed_data{1, 1}{2, 3}(4,2)
x2n = windowed_data{1,1}{2,4}(4,2)
y2n = windowed_data{1, 1}{2, 5}(4,2)
z2n = windowed_data{1, 1}{2, 6}(4,2)
In my full data set all the rows have different sized cells meaning that I need to cycle through all the elements in all the columns in the cells. I hope this makes it a bit more clear.
I am very thankful for the help!