Info
此问题已关闭。 请重新打开它进行编辑或回答。
How do I calculate the distance between 3D coordinate points using cell array?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Johan
2022-2-7
Hello,
you can use the cellfun function which applies a defined function on each cell of a cell array:
% make some data
concatenated_cell = cellfun(@(x) [rand(2,6)],cell(2,6),'UniformOutput',false)
% compute the norm for each cell and force output as cell with,'UniformOutput',false
distance_as_cell = cellfun(@(x) norm(x(1,1:3)-x(1,4:6)),concatenated_cell,'UniformOutput',false)
To do that to cells of a cell array you can concatenate the cellfun function:
Data(1)= {concatenated_cell};
Data(2)= {cellfun(@(x) [rand(2,6)],cell(2,6),'UniformOutput',false)};
Distance_in_Cells = cellfun(@(y) cellfun(@(x) norm(x(1,1:3)-x(1,4:6)),y,'UniformOutput',false), Data,'UniformOutput',false)
Distance_in_Cells{1}
I used the norm function here, it does the same as sqrt(sum(x-y)^2) as far as I know.
Hope this helps.
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!