Matlab Reversi 'valid move' check - matlab

Having issues with my move checker currently, it seems that the failsafe I put in my code to make it not look outside the bounds of the matrix isn't working, any suggestions?
There is also the issue that it doesn't seem to be working (i.e. I am still able to place pieces wherever I want!). The code I use earlier up is listed below as well
code:
function legal = legalMove()
d_l = [0, -1];
d_r = [0, 1];
d_u = [-1, 0];
d_d = [1, 0];
d_ul = [-1, -1];
d_ur = [-1, 1];
d_dl = [1, -1];
d_dr = [1, 1];
directions = {'d_l' 'd_ul' 'd_u' 'd_ur' 'd_r' 'd_dr' 'd_d' 'd_dl'};
valid_moves = zeros(8,8);
for ci = 1:8
for cj = 1:8
if game_state(ci,cj) == 0 %check element = 0
for count = 1:8
d = eval( directions{count} );
ti = ci+d(1);
tj = cj+d(2);
% Check if out of the board
if (ti > 8 || ti < 1) || (tj > 8 || tj < 1)
break
else
% Number of enemy pieces you went over
cnt = 0;
selected = game_state(ti, tj);
% Move while going over enemy pieces
while selected == player_number * -1
ti = ti + d(1);
tj = tj + d(2);
selected = game_state(ti, tj);
% Check if out of the board
if (ti > 8 || ti < 1) || (tj > 8 || tj < 1)
break
else
end
% Count pieces you went over
cnt = cnt + 1;
end
end
% Check if you moved over enemy pieces & whether you landed on your piece
if selected == player_number
valid_moves(ti,tj) = 1;
else
end
end
else
end
end
end
if ~isempty(valid_moves)
legal = 1;
else
legal = 0;
end
end
Error returned when done # boundries:
Attempted to access game_state(0,7); index must be a positive integer or
logical.
Error in umpire/legalMove (line 217)
selected = game_state(ti, tj);
Error in umpire/buttonPress (line 85)
legal = legalMove();
other piece:
function buttonPress(hObject, eventdata)
ended = game_is_over();
if ended == 1;
setAllInactive();
winner = calc_winner();
if winner == -1;
set(stat_text,'string','Winner is White! Restart?')
elseif winner == 1;
set(stat_text,'string','Winner is Black! Restart?')
else
set(stat_text,'string','Game is a tie! Restart?')
end
else
end
legal = legalMove();
if legal ~= 1;
set(stat_text,'Illegal move! Try again')
return
else
end
game_state(get(hObject,'userdata')) = player_number;
drawScreen();
player_number = player_number * -1;
end

In the second place where you assign variable selected (near the end of the function legalMove), you have out-of-board check in the wrong place.
Here's a fixed version
% ...
% Number of enemy pieces you went over
cnt = 0;
selected = game_state(ti, tj);
% Move while going over enemy pieces
while selected == player_number * -1
ti = ti + d(1);
tj = tj + d(2);
% Check if out of the board
if (ti > 8 || ti < 1) || (tj > 8 || tj < 1)
break
else
end
selected = game_state(ti, tj);
% Count pieces you went over
cnt = cnt + 1;
end
end
% Check if you moved over enemy pieces & whether you landed on your piece
if selected == player_number
valid_moves(ti,tj) = 1;
else
end
end

Related

MATLAB 3D lines are invisible

Hello I'am suffered from a problem
As you can see I want a draw 3D graph.
Problem is when I draw sphere lines are invisible.
Here is simple version of my source
clear all; close all; clc
n=1;
n_inner_drone=3;
n_outter_drone=2;
length=100;
initial_d = zeros(1,n);
inner_x = zeros(n_inner_drone,n);
inner_y = zeros(n_inner_drone,n);
inner_z = zeros(n_inner_drone,n);
outter_x = zeros(n_outter_drone,n);
outter_y = zeros(n_outter_drone,n);
outter_z = zeros(n_outter_drone,n);
radius= length;
disp('test');
%%%%%%%%%%%%%%%%%%%%%% Sphere
% figure()
% [x, y, z] = sphere;
% h = surfl(x*length, y*length, z*length);
% hSurf = surf(X,Y,Z,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
% set(h, 'FaceAlpha', 0.05)
% surf(x*length, y*length, z*length,
% shading interp
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:n_inner_drone
k=1;
while 1
x_temp= randi([-length, length], 1, 1);
y_temp= randi([-length, length], 1, 1);
z_temp= randi([-length, length], 1, 1);
dist = sqrt(x_temp^2 + y_temp^2 + z_temp^2);
if dist<radius
if i==1
initial_d(k) = dist;
end
inner_x(i,k) = x_temp;
inner_y(i,k) = y_temp;
inner_z(i,k) = z_temp;
k = k+1;
end
if k == n+1, break, end
end
end
ideal_direction_length = ones(1,n);
ideal_direction_length = ideal_direction_length * length;
ideal_direction_length = ideal_direction_length - initial_d;
k=1;
random_x = inner_x(1,:);
random_y = inner_y(1,:);
random_z = inner_z(1,:);
random_moving_distance = zeros(1,n);
moving_distance = 0;
trigger = 0;
while 1
if trigger == 0
direction = randi([1, 6], 1, 1);
trigger = 1;
end
if direction == 1
random_x(k) = random_x(k) + 1;
elseif direction == 2
random_x(k) = random_x(k) - 1;
elseif direction == 3
random_y(k) = random_y(k) + 1;
elseif direction == 4
random_y(k) = random_y(k) - 1;
elseif direction == 5
random_z(k) = random_z(k) + 1;
elseif direction == 6
random_z(k) = random_z(k) - 1;
end
dist = sqrt(random_x(k)^2 + random_y(k)^2 + random_z(k)^2);
moving_distance = moving_distance+1;
%%%%%%%%%% Line
plot3(random_x(n),random_y(n),random_z(n),'k+')
%%%%%%%%%%%%%%%
if dist>radius
random_moving_distance(k) = moving_distance;
k = k+1;
moving_distance = 0;
trigger = 0;
end
if k == n+1, break, end
end
plot3(inner_x(1,n),inner_y(1,n),inner_z(1,n),'r*')
for k=2:n_inner_drone
plot3(inner_x(k,n),inner_y(k,n),inner_z(k,n),'b*')
end
for k=1:n_outter_drone
plot3(outter_x(k,n),outter_y(k,n),outter_z(k,n),'k*')
end
At the first, I suspected I worngly draw lines, but without sphere I can see lines as fig2.
Those anyone who knows about this problem.
Please answer to me and I will very appericiate about it.
Thanks for reading.
I think it is because:
plot3(gravity_x(n),gravity_y(n),gravity_z(n))
is not a line. Its a single point.
plot3(gravity_x(n:n+1),gravity_y(n:n+1),gravity_z(n:n+1))
is a line.

I need to implement a WSN simulator for group leader selection, using matlab

I am trying to implement a wireless sensor network simulator on matlab and I need your help.
This is exactly what I want to do:
Deploy nodes randomly in a 2D plane.
Model a group leader election algorithm using two conditions:
a) Energy: generate random energy values associated with each of the sensors, the sensor node with the maximum energy has higher probability of being selected as leader.
b) Proximity: the sensor node that is mostly surrounded by neighboring nodes has higher probability of being selected.
So, for a random node that has maximum energy and more neighbors can be selected as leader and plotted with the rest of the nodes in a different color.
I have been working on this, trying to develop my code all to no avail. I am not really good with matlab coding, I am still learning.
Please guys, I need any help I can get on this, my deadline is imminent.
Thanks,
Ike
First of all building a wireless networks simulator using matlab is by far not the best choice to do. its better to use specialized simulators such as NS2 or Omnet++. however, I have developed once a simple m-file code to randomly deploy points in a square are and try to link between these points according to the distance between these points. the points are assumed to be sensor nodes.
try to modify on it to get what you need. please vote up if this answer was useful to you:
W=0;X=0;Y=0;Z=0;
nodedistance = zeros();
maxx = 400; maxy=400; maxn = 50;
q = zeros(maxn);
e = rand(maxn,1)*100;
nodeloc = rand(maxn, 2)* maxx;
node(maxn) = struct('NodeNum',zeros(maxn),'nEnergy',zeros(maxn),'Loc',[zeros(maxn,1), zeros(maxn,2)]);
rss(maxn,maxn) = struct('NodeNumber',zeros(maxn),'NodeDistance',zeros(maxn));
for a = 1:100,
m = 2;
node = struct();
rss = struct();
nodedistance = zeros();
maxx = 400; maxy=400; maxn = 50;
q = zeros(maxn);
e = rand(maxn,1)*100;
nodeloc = rand(maxn, 2)* maxx;
for i = 1: maxn,
node(i)=struct('NodeNum',i,'nEnergy',e(i),'Loc',[nodeloc(i, 1), nodeloc(i, 2)]);
end
for i = 1:maxn,
for j = 1:maxn,
rss(i,j) = struct('NodeNumber',i,'NodeDistance',sqrt((node(i).Loc(1)- node(j).Loc(1))^2+(node(i).Loc(2)-node(j).Loc(2))^2));
end
end
for i = 1:maxn,
for j = 1:maxn,
nodedistance(i,j)=rss(i,j).NodeDistance;
end
end
for i = 1:maxn,
for j = 1:maxn,
if (node(i).nEnergy > 0) && (node (j).nEnergy > 0) && (0 < nodedistance(i,j) && nodedistance(i,j) <= 30)
if (node(i).nEnergy < node (j).nEnergy) %|| ( (node(i).nEnergy == node (j).nEnergy )&& (0 < nodedistance(i,j) && nodedistance(i,j)) < 30) %|| (0 < nodedistance(i,j) && nodedistance(i,j) <= 30)
q(i,j) = 1;
elseif node(i).nEnergy == node (j).nEnergy && 0 < nodedistance(i,j) && nodedistance(i,j) < 30
q(i,j) = 1;
else if 0 < nodedistance(i,j) && nodedistance(i,j) <= 30
q(i,j) = 1;
else
q(i,j) = 0;
end
end
end
end
end
end
for i = 1:maxn,
for j = 1:maxn,
if q(i,j) == 1
q(j,i) = 1;
end
end
end
colordef white,
figure(1);
axis equal
for i = 1:maxn,
hold on
box on;
plot(nodeloc(i, 1), nodeloc(i, 2), 'k.', 'MarkerSize', 5);
lscatter(nodeloc(i, 1),nodeloc(i, 2),i);
grid on;
end
gplot(q,nodeloc,'r-');
c = q;
b = 0; zeros(maxn);
check = 1;
while (check)
for m = 2:30
p = size(b)
b = zeros(maxn,maxn);
for i = 1:maxn,
for j = 1:maxn,
if i ~= j
if c(i,j) == 0
for k = 1:maxn,
if c(i,k) >0 && q(k,j) >0
b(i,j) = m;
break
else b(i,j) = 0;
end
end
end
end
end
end
f = size(b)
c = c + b;
if b ==0
check = 0;
else b = 0;% while loop here the condition.
m = m + 1;
end
end
k = 0;
for i = 1:maxn,
for j = 1:maxn,
k = k + c(i,j);
end
end
average_hop = k/(maxn*(maxn-1));
o = 0;
for i = 1:maxn,
for j = 1:maxn,
if c(i,j) ~= 0
o = o + 1;
end
end
end
c(~c) = nan;
r=max(max(c));
t=min(min(c));
clear
end
formatSpec1 = 'The number of hops from node %d to node %d is %d \n';
formatSpec2 = 'The average number of hops is %.4f, the maximum hop-count is %d, the minimum hop-count is %d \n';
formatSpec3 = 'The number of created paths is %d \n';
fileID = fopen('Paths11.txt','w');
fprintf(fileID,formatSpec1,i,j,c(i,j));
fprintf(fileID,formatSpec2,average_hop,r,t);
fprintf(fileID,formatSpec3,o);
clear;
X = X+average_hop;
Y = Y+r;
z = Z+t;
W = W+o;
end;
formatSpec1 = 'The number of hops from node %d to node %d is %d \n';
formatSpec2 = 'The average number of hops is %.4f, the average maximum hop-count is %.2f, the minimum hop-count is %d \n';
formatSpec3 = 'The average number of created paths is %.2f \n';
fileID = fopen('Energy-50-AODV.txt','w');
fprintf(fileID,formatSpec1,i,j,c(i,j));
fprintf(fileID,formatSpec2,X/a,Y/a,t);
fprintf(fileID,formatSpec3,W/a);
clear;
This is not a get-your-homework-done-for-free site; you'll have to do most of the work yourself. Here are my tips:
Don't leave your assignments to the last minute.
Study the MATLAB code of other people working in your field, and refer to the MATLAB help files (hit F1) when you get stuck. Here's a fruitful search query to point you in the right direction: wsn OR "sensor network" clustering matlab code site:edu

Matlab coder "Error indenting generated C code"

I am Trying to convert a MATLAB code to C++ using MATLAB coder but this error apears:
Error indenting generated C code
The error points to the name of the function itself and has no more explanations in it. can someone tell me what is this error?
here is the function i want to conver:
function [Report_Clustered,ClusterCounter_new]=InitClusterGenerator_test(Report_In,~,FreqEpsilon,DegreeEpsilon,~,ClusterCounter_old, BlockCount, Report_old)
Report_M = zeros(size(Report_In,1),size(Report_In,2),4);
for i=1:size(Report_In,1)
for j=1:size(Report_In,2)
Report_M(i,j,1)=Report_In(i,j,1);
Report_M(i,j,2)=Report_In(i,j,2);
Report_M(i,j,3)=0; % Cluster number that the point belongs to.
Report_M(i,j,4)=0;
Report_In{i,j}
end
end
ClusterCounter = 0;
for i=1:size(Report_M,1)
for j=1:size(Report_M,2)
if (Report_M(i,j,3) == 0)
ClusterCounter = ClusterCounter + 1;
Report_M(i,j,3) = ClusterCounter;
for ii=1:size(Report_M,1)
for jj=1:size(Report_M,2)
if (Report_M(ii,jj,3) == 0)
if (abs(Report_M(i,j,1)-Report_M(ii,jj,1))<FreqEpsilon &&...
(abs(Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(-360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon))
Report_M(ii,jj,3) = ClusterCounter;
end
end
end
end
end
end
end
if (BlockCount> 20 && ClusterCounter<4)
warning = 1;
end
ClusterCounter_new = ClusterCounter;
%clear Report_new;
flag = 0;
Report_new = zeros(ClusterCounter,size (Report_M, 2),4);
index = zeros(1, ClusterCounter_new);
for i = 1: size (Report_M, 1)
for j = 1: size (Report_M, 2)
for k = 1: ClusterCounter_new
if (Report_M(i,j,3) == k)
index(1,k) = index(1,k) + 1;
Report_new(k,index(1,k), 1:3) = Report_M(i,j,1:3);
flag = flag + 1;
end
end
end
end
for j = 1: size (Report_new, 2)
for i = 1: size (Report_new, 1)
if (Report_new(i,j,1) == 0)
Report_new(i,j,1:3) = Report_new(i,1,1:3);
end
end
end
%Report_new = Report;
MedoidF_old = zeros(1, size(Report_old,1));
MedoidA_old = zeros(1, size(Report_old,1));
for i=1:size(Report_old,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_old,2)
SumF = SumF + Report_old(i,j,1);
SumA = SumA + Report_old(i,j,2);
if Report_old(i,j,2) > MaxAngle
MaxAngle = Report_old(i,j,2);
elseif Report_old(i,j,2) < MinAngle
MinAngle = Report_old(i,j,2);
end
end
MedoidF_old(1, i) = SumF/size(Report_old,2);
if (MaxAngle - MinAngle) > 350
MedoidA_old(1, i) = 0;
else
MedoidA_old(1, i) = SumA/size(Report_old,2);
end
end
MedoidF_new = zeros(1, size(Report_new,1));
MedoidA_new = zeros(1, size(Report_new,1));
for i=1:size(Report_new,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_new,2)
SumF = SumF + Report_new(i,j,1);
SumA = SumA + Report_new(i,j,2);
if Report_new(i,j,2) > MaxAngle
MaxAngle = Report_new(i,j,2);
elseif Report_new(i,j,2) < MinAngle
MinAngle = Report_new(i,j,2);
end
end
MedoidF_new(1, i) = SumF/size(Report_new,2);
if (MaxAngle - MinAngle) > 350
MedoidA_new(1, i) = 0;
else
MedoidA_new(1, i) = SumA/size(Report_new,2);
end
end
TempCluster = zeros(1, size(Report_new, 1));
CurrentCluster = ClusterCounter_old;
for i = 1: 1: size(Report_new,1)
for j = 1: 1: size(Report_old,1)
if (abs(MedoidF_old(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_old(j,1,3);
end
end
%%this part is for seperating the clusters which where in the collision state in the past time
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%first search if the new cluster is a part of a newly found cluster found before this one
for j = 1: 1: i-1
if (abs(MedoidF_new(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_new(j,1,3);
end
end
end
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%new cluster is just began so it needs a new index
CurrentCluster = CurrentCluster + 1;
ClusterCounter_new = CurrentCluster;
TempCluster(1,i) = CurrentCluster;
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1; % Alive
end
end
end
NewClusters = zeros(1, size (Report_new, 1));
for i = 1: size(Report_new, 1)
NewClusters (1,i) = Report_new(i,1,3);
end
OldClusters = zeros(1, size (Report_old, 1));
OldClustersLine = zeros(1, size (Report_old, 1));
for i = 1: size(Report_old, 1)
OldClusters (1,i) = Report_old(i,1,3);
OldClustersLine (1, i) = i;
end
NumberOfDead = 0;
%clear AddDead;
AddDead = zeros (16,size(Report_new, 2),4);
if (BlockCount>10)
for i = 1: size (OldClusters, 2)
IsDead = 1;
for j = 1: size (NewClusters, 2)
if OldClusters(1, i) == NewClusters(1,j)
IsDead = 0;
end
end
if (IsDead == 1)
NumberOfDead = NumberOfDead + 1;
%clear TempLine;
TempLine = zeros(1, size(Report_old,2), 4);
TempLine(1,:,1:3) = Report_old(OldClustersLine(1, i),:,1:3);
for k= 1: size(TempLine, 2)
TempLine(1,k,4) = 0; % Dead
end
TempSize = size(TempLine, 2);
Thresh = size(Report_new, 2);
if (TempSize >= Thresh)
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
else
for l = 1: Thresh-TempSize
TempLine(1, TempSize+l, 1:4) = TempLine(1, TempSize, 1:4);
end
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
end
end
end
xR = size (Report_new,1);
if (NumberOfDead == 0)
Report_Clustered = zeros (size(Report_new,1),size(Report_new,2),size(Report_new,3));
else
Report_Clustered = zeros (size(Report_new,1) + NumberOfDead,size(Report_new,2),size(Report_new,3));
end
Report_Clustered (1:size(Report_new,1), :, :) = Report_new(:,:,:);
for i = 1: NumberOfDead
Report_Clustered(xR + i, :) = AddDead(i, :);
end
end
and I'm using matlab 2012a
Tnx.
From what you've said in the comments, it appears that you simply need to call
clear functions
from the command line before recompiling the function to allow Matlab to overwrite the files. See this Matlab forum or the documentation for clear for more detail.

Reversi Valid Move Checker

I've been doing an assignment which requires me to recreate reversi in matlab.
I have hit a break point where I can't manage to figure out how to do a section of code without turning it into linear indexing.
Essentially what I need to do is figure out a way to add a direction vector (i.e. [-1;0] to go vertically), to my already existing game_state vector in order to find the piece of data above it.
function legal = legalMove()
d_l = [0, -1];
d_r = [0, 1];
d_u = [-1, 0];
d_d = [1, 0];
d_ul = [-1, -1];
d_ur = [-1, 1];
d_dl = [1, -1];
d_dr = [1, 1];
directions = [d_l d_ul d_u d_ur d_r d_dr d_d d_dl];
valid_moves = zeros(8,8);
for ci = 1:8
for cj = 1:8
if game_state(ci,cj) == 0 %check element = 0 before continuing
for count = 1:8
d = directions(count);
selected =
while selected == player_number * -1 %while the selected piece is of enemy type
%move as long as you find your opponents stones
if you found at least 1 opponent stone and you end up on your own stone
else
end
end
end
else
end
end
end
end
My second issue after that, is the actual loop itself.
While I thought that a while loop could be used in order to find out whether to stay on the current direction, I cannot seem to figure out a way to do it myself.
Any assistance would be appreciated.
Thanks for your time!
Change directions to cell-array that contains strings:
...
directions = { 'd_l' 'd_ul' 'd_u' 'd_ur' 'd_r' 'd_dr' 'd_d' 'd_dl' };
...
Then next part of the code would go something like this (fill in the blanks!):
for count = 1:8
d = eval( directions{count} );
ti = ci+d(1);
tj = cj+d(2);
% Check if out of the board
...
% Number of enemy pieces you went over
cnt = 0;
selected = gamestate(ti, tj)
% Move while going over enemy pieces
while selected == player_number * -1
ti = ti + d(1);
tj = tj + d(2);
selected = gamestate(ti, tj);
% Check if out of the board
...
% Count pieces you went over
cnt = cnt + 1;
end
% Check if you moved over enemy pieces & whether you landed on your piece
...
end

Matlab error : Subscript indices must either be real positive integers or logicals

I have the following error in MATLAB:
??? Subscript indices must either be real positive integers or
logicals.
Error in ==> Lloyd_Max at 74 D(w_count) = mean((x -
centers(xq)).^2);
This is my code :
function [ xq,centers,D ] = Lloyd_Max( x,N,min_value,max_value )
%LLOYD_MAX Summary of this function goes here
% Detailed explanation goes here
x = x';
temp = (max_value - min_value)/2^N;
count=1;
for j=0:temp:((max_value - min_value)-temp),
centers(count) = (j + j + temp )/2;
count = count + 1;
end
for i=1:length(centers),
k(i) = centers(i);
end
w_count = 0;
while((w_count < 2) || (D(w_count) - D(w_count - 1) > 1e-6))
w_count = w_count + 1;
count1 = 2;
for i=2:(count-1),
T(i) = (k(i-1) + k(i))/2;
count1 = count1 +1 ;
end
T(1) = min_value;
T(count1) = max_value;
index = 1;
for j=2:count1,
tempc = 0;
tempk = 0;
for k=1:10000,
if(x(k) >= T(j-1) && x(k) < T(j))
tempk = tempk + x(k);
tempc = tempc + 1;
end
end
k(index) = tempk;
k_count(index) = tempc;
index = index + 1;
end
for i=1:length(k),
k(i) = k(i)/k_count(i);
end
for i=1:10000,
if (x(i) > max_value)
xq(i) = max_value;
elseif (x(i) < min_value)
xq(i) = min_value;
else
xq(i) = x(i);
end
end
for i=1:10000,
cnt = 1;
for l=2:count1,
if(xq(i) > T(l-1) && xq(i) <= T(l))
xq(i) = cnt;
end
cnt = cnt +1 ;
end
end
D(w_count) = mean((x - centers(xq)).^2);
end
end
and i call it and have these inputs :
M = 10000
t=(randn(M,1)+sqrt(-1)*randn(M,1))./sqrt(2);
A= abs(t).^2;
[xq,centers,D] = Lloyd_Max( A,2,0,4 );
I tried to comment the while and the D, Results :
I got the xq and the centers all normal, xq in the 1-4 range, centers 1-4 indexes and 0.5-3.5 range.
I dont know whats going wrong here...Please help me.
Thank in advance!
MYSTERY SOVLED!
Thank you all guys for your help!
I just putted out of the while the for loop :
for i=1:10000,
if (x(i) > max_value)
xq(i) = max_value;
elseif (x(i) < min_value)
xq(i) = min_value;
else
xq(i) = x(i);
end
end
and it worked like charm.... this loop was initilizing the array again. Sorry for that. Thank you again!
There is an assignment xq(i) = x(i) somewhere in the middle of your function, but you pass A as x from outside where you calculate A from t which is sampled by randn, so you can't promise xq is an integer.
I'm not sure exactly what you are aiming to do, but your vector xq does not contain integers, it contains doubles. If you want to use a vector of indices as you do with centers(xq), all elements of the vector need to be integers.
Upon a little inspection, it looks like xq are x values, you should find some way to map them to the integer of the closest cell to which they belong (i'm guessing 'centers' represents centers of cells?)