MATLAB: How to get the position of nodes (x and y coordinates) stored in a logical array

binaryindices

i receive the following error
Error using get
Invalid handle
Error in final (line 81)
nodloc= get(CPP,'Position');
clc;
clear all;
close all; % clear memory and command window
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
k=5; % Number of clusters
%area dimension
xm=300;
ym=300;
%number of transmitted packets
packet=4000;
%x and y Coordinates of the Sink
sink.x=0.5*xm;
sink.y=0.5*ym;
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.1;
%Eelec=Etx=Erx
Eelec=50*0.000000001;
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000001;
%maximum number of rounds
rmax=10;
%% Generate random data points
n=100; % number of samples or points
rx=100*rand(n,1); % vector of random data x
ry=100*rand(n,1); % vector of random data y
%% Initial values of the centroids
centroidsx=rx( ceil(rand(k,1)*size(rx,1)) ,:); % initial cluster centers x
centroidsy=ry( ceil(rand(k,1)*size(ry,1)) ,:); % initial cluster centers y
%% Iterative process
for r=1:rmax
r
for i=1:1:n
S(i).E=Eo;
for j=1:k
if r==1
diste(i,j)=sqrt(((rx(i))-(centroidsx(j)))^2+((ry(i))-(centroidsy(j)))^2);
d(j)=sqrt((centroidsx(j))-(sink.x))^2+(((centroidsy(j))-(sink.y))^2);
%the sample of the jth centroid
else
diste(i,j)=sqrt(((rx(i))-(Cclustx(j)))^2+((ry(i))-(Cclusty(j)))^2);
d(j)=sqrt((Cclustx(j))-(sink.x))^2+(((Cclusty(j))-(sink.y))^2);
end
end
% Define clusters
[minidist, CN] = min(diste(i,1:k)); % minimum distance and the
% cluster which the sample belongs to
Distance(r,i)=minidist;
Cln(r,i)=CN;
end
% Recompute the clusters center
Ethreshold=0.05;
for q=1:k
if S(i).E>Ethreshold
PC=(Cln(r,:)==q); % Position of the points of the cluster
Cpoints(q,:)=PC; % Points of the cluster
px(q,:)=mean(rx(PC));
%Cclustx(q,:)=mean(rx(PC)); % New cluster centers in x
Cclustx(q,:) = min(rx(rx >= px(q,:)));
py(q,:)=mean(ry(PC));
Cclusty(q,:) = min(ry(ry >= py(q,:)));
%Cclusty(q,:)=mean(ry(PC)); % New cluster centers in y
end
end
SCCx(r,:)=Cclustx; % center of the cluster at each iteration in x
SCCy(r,:)=Cclusty; % center of the cluster at each iteration in y
CPP(r,:,:)=Cpoints; % points of the cluster at each iteration
nodes=double(CPP);
nodloc= get(CPP,'Position');
% nodeloc=find(nodes)
% nodesloc=find(nodes==1)
%Number of dead nodes
dead=0;
figure(1);
%Computation of do
do=sqrt(Efs/Emp);%distance between cluster head and base station
%Calculation of Energy dissipated
for i=1:1:n
if (diste(i,j)>do)
S(i).E=S(i).E- ( (ETX+EDA)*(packet) + Emp*packet *( diste(i,j).^4 ));
end
if (diste(i,j)<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(packet) + Efs*packet*( diste(i,j).^2 ));
end
Energy_disp(r+1) = S(i).E;
%checking if there is a dead node
if (S(i).E<=0)
dead(i)=dead(i)+1;
end
end
if (dead == n)
break;
end
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
sum=0;
for i=1:1:n
if(S(i).E>0)
sum=sum+S(i).E;
end
end
avg=sum/(n-1);
STATISTICS(r+1).AVG=avg;
sum;
end
%% Plot of the movements of the centroids and clusters
CV= 'o*+s^v.db+c+m+k+yorobocomokoysrsbscsmsksy'; % Color Vector
for r=1:rmax+1
figure (1)
if r==1
plot(rx,ry,'o','LineWidth',1.5);
hold on;
plot(centroidsx,centroidsy,'*k','LineWidth',6.5);
hold off
else
for j=1:k
plot(rx(CPP(r-1,j,:)),ry(CPP(r-1,j,:)),CV(j),'LineWidth',2); % Plot points with determined color and shape
hold on
end
plot(SCCx(r-1,:),SCCy(r-1,:),'*k','LineWidth',6); hold off
end
grid on
pause(0.8)
end
figure(2);
for r=0:1:rmax-1
ylabel('Average Energy of Each Node');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'blue');
hold on;
end
figure(3);
for r=0:1:rmax-1
ylabel('Number of Dead Nodes');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'blue');
hold on;
end

Best Answer

You can use get if it's a handler (variable that represent an object (plot or somerthing))
CPP is array of logical type
CPP(r,:,:)=Cpoints; % points of the cluster at each iteration
nodes=double(CPP);
nodloc= get(CPP,'Position');
You can use find to get nonzero values as you did in next line. But add squeeze to get 2D matrix
[ii,jj] = find(squeeze(CPP));