Effacer les filtres
Effacer les filtres

I am trying to calculate the KL divergence between the two point clouds but I am getting the following error, please help.

8 vues (au cours des 30 derniers jours)
% Read the first point cloud
pc1 = pcread('C:\Users\amitv\Downloads\fragment.ply');
% Read the second point cloud
pc2 = pcread('C:\Users\amitv\Downloads\fragment.ply');
% Downsample the first point cloud
pc1_ds = pcdownsample(pc1,'gridAverage',0.1);
% Downsample the second point cloud
pc2_ds = pcdownsample(pc2,'gridAverage',0.1);
% Show the downsampled first point cloud
figure; pcshow(pc1_ds);
title('Downsampled First Point Cloud');
% Show the downsampled second point cloud
figure; pcshow(pc2_ds);
title('Downsampled Second Point Cloud');
% reshape the location matrix into a column vector
location1 = reshape(pc1_ds.Location,[],1);
location2 = reshape(pc2_ds.Location,[],1);
% Calculate the PDF of the downsampled first point cloud using KDE
pdf1 = fitdist(location1,'kernel','Kernel','normal','Support','unbounded','Width',1,'Mu',0,'Sigma',1);
% Calculate the PDF of the downsampled second point cloud using KDE
pdf2 = fitdist(location2,'kernel','Kernel','normal','Support','unbounded','Width',1,'Mu',0,'Sigma',1);
% Show the PDF of the downsampled first point cloud
[f1,xi1] = ksdensity(location1);
figure;
plot(xi1,f1);
title('PDF of Downsampled First Point Cloud');
% Show the PDF of the downsampled second point cloud
[f2,xi2] = ksdensity(location2);
figure;
plot(xi2,f2);
title('PDF of Downsampled Second Point Cloud');
% Define the KL divergence function
function dkl = kl_divergence(p, q)
dkl = sum(p .* (log(p) - log(q)));
end
% Calculate the KL divergence between the two PDFs
kl = kl_divergence(pdf1, pdf2);
% Print the KL divergence
fprintf('KL divergence: %f\n', kl);
errror : Error: File: kld12.m Line: 49 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "kl_divergence" function definition to before the first
local function definition.

Réponses (1)

Ashu
Ashu le 20 Mar 2023
Hi Amit,
Moving this function definition to the end of the script will resolve your error.
% Define the KL divergence function
function dkl = kl_divergence(p, q)
dkl = sum(p .* (log(p) - log(q)));
end

Community Treasure Hunt

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

Start Hunting!

Translated by