JPEG Encoder bug - matlab

I am trying to make my own JPEG encoder (relying on only low level operations). I am mostly done, but I get an error where the image is coming out all wrong. I think it is due to my understanding of the format itself. I have a 16x16 test image, and I use the header straight from the image (I replaced the quantization and huffman tables to match the ones I am using, but I think I did that correctly since I could pretty clearly make out what I need to replace and what I leave). My problem is understanding the data part after SOS. So, we have our 8x8, DCT-II'd, Quantizied block, we read it zig-zag and grab the first digit and its our DC. We then see if its positive or negative and its general length. (length = ceil(log2(abs(x))). We then use the length to pull the huffman code from the table, and then write those bits (I have a buffer so every time I get 8 bits I write a byte). Then you go through the RLE of AC (which we got from traversing the rest of the 8x8 in zig zag and writing how many 0's we saw before). If the number of zeros before is > 16 I put the 0xf0, do I need to then put a val of 0? Or do I just leave it? assuming its a normal number (take 25 for example), I write the huffman code from the run//size key dict, then I just put 011001 (2's comp 25) right? Then I move on till I hit 0's till the end and put the 0000/0000 code in (which let's say it translates to 00 so we just append 00 to the file). Then I rinse and repeat? Do I need to do anything between 8x8 blocks? Does the order go DC - Y comp, AC - Y comp, DC - Cb comp, AC Cb comp, DC Cr comp, AC - Cr comp, next block? Do I do anything between blocks? And for the DC "previous diff" situation, does that mean previous DC val in that component? Like the previous Y comp's DC? At the end I pad 1's till I complete a byte and then write the EOI tag right?
[for loop handling the 8x8, quant, dft ect.]
% Huffman Encoding
% DC Encoding
if channel == 1
val = freq(1) - prev_DC_Y;
elseif channel == 2
val = freq(1) - prev_DC_Cb;
else
val = freq(1) - prev_DC_Cr;
end
if val ~= 0
size_of_bits = log2(abs(val));
size_of_bits = ceil(size_of_bits);
else
size_of_bits = 0;
end
if channel == 1
num_bits_to_append = cell2mat(dc_lum{size_of_bits+1}(1));
bits_to_append = cell2mat(dc_lum{size_of_bits+1}(2));
else
num_bits_to_append = cell2mat(dc_cbcr{size_of_bits+1}(1));
bits_to_append = cell2mat(dc_cbcr{size_of_bits+1}(2));
end
for i1 = 1:num_bits_to_append
bits(bits_on_deck+1) = bits_to_append(i1);
bits_on_deck = bits_on_deck + 1;
end
if val < 0
bits(bits_on_deck+1) = 1;
bits_on_deck = bits_on_deck + 1;
val = abs(val) - 1;
else
bits(bits_on_deck+1) = 0;
bits_on_deck = bits_on_deck + 1;
end
for i1 = 1:size_of_bits
if bitand(bitshift(1,i1-1), val) ~= 0
bits(bits_on_deck+1) = 1;
bits_on_deck = bits_on_deck + 1;
else
bits(bits_on_deck+1) = 0;
bits_on_deck = bits_on_deck + 1;
end
while bits_on_deck >= 8
manage_bits(fileID, bits);
bits_on_deck = bits_on_deck - 8;
end
end
if channel == 1
prev_DC_Y = val;
elseif channel == 2
prev_DC_Cb= val;
else
prev_DC_Cr = val;
end
% AC Encoding
i1 = 1;
code = 1;
while code ~= 0
code = order(i1)
val = order(i1+1)
size_of_bits = ceil(log2(abs(val)));
if channel == 1
num_bits_to_append = cell2mat(ac_lum{code+1}(1));
bits_to_append = cell2mat(ac_lum{code+1}(2));
else
num_bits_to_append = cell2mat(ac_cbcr{code+1}(1))
bits_to_append = cell2mat(ac_cbcr{code+1}(2))
end
for i2 = 1:num_bits_to_append
bits(bits_on_deck+1) = bits_to_append(i2);
bits_on_deck = bits_on_deck + 1;
end
while bits_on_deck >= 8
manage_bits(fileID, bits);
bits_on_deck = bits_on_deck - 8;
end
if code == 0 break
end
if val < 0
bits(bits_on_deck+1) = 1;
bits_on_deck = bits_on_deck + 1;
val = abs(val) - 1;
else
bits(bits_on_deck+1) = 0;
bits_on_deck = bits_on_deck + 1;
end
for i2 = 1:size_of_bits
if bitand(bitshift(1,i2-1), val) ~= 0
bits(bits_on_deck+1) = 1;
bits_on_deck = bits_on_deck + 1;
else
bits(bits_on_deck+1) = 0;
bits_on_deck = bits_on_deck + 1;
end
while bits_on_deck >= 8
manage_bits(fileID, bits);
bits_on_deck = bits_on_deck - 8;
end
end
i1 = i1 + 2;
end
end
end
end
if bits_on_deck ~= 0
while bits_on_deck < 8
bits(bits_on_deck+1) = 1;
bits_on_deck = bits_on_deck + 1;
end
end
manage_bits(fileID, bits);
fwrite(fileID, [0xff, 0xd9]);
fclose(fileID);
Target
Actual

Related

MATLAB 'parfor' Loops Very Slow When Compared With 'for' loop

I have a script that I'm running, and at one point I have a loop over n objects, where I want n to be fairly large.
I have access to a server, so I put in a parfor loop. However, this is incredibly slow compared with a standard for loops.
For example, running a certain configuration ( the one below ) with the parfor loop on 35 workers took 68 seconds, whereas the for loop took 2.3 seconds.
I know there's stuff to do with array-broadcasting that can cause issues, but I don't know a lot about this.
n = 20;
r = 1/30;
tic
X = rand([2,n-1]);
X = [X,[0.5;0.5]];
D = sq_distance(X,X);
A = sparse((D < r) - eye(n));
% Infected set I
I = n;
[S,C] = graphconncomp(A);
compnum = C(I);
I_new = find(C == compnum);
I = I_new;
figure%('visible','off')
gplot(A,X')
hold on
plot(X(1,I),X(2,I),'r.')
hold off
title('time = 0')
axis([0,1,0,1])
time = 0;
t_max = 10; t_int = 1/100;
TIME = 1; T_plot = t_int^(-1) /100;
loops = t_max / T_plot;
F(loops) = struct('cdata',[],'colormap',[]);
F(1) = getframe;
% Probability of healing in interval of length t_int
heal_rate = 1/3; % (higher number is faster heal)
p_heal = t_int * heal_rate;
numhealed = 0;
while time < t_max
time = time+t_int;
steps = poissrnd(t_int,[n,1]);
parfor k = 1:n
for s = 1:steps(k)
unit_vec = unif_unitvector;
X_new = X(:,k) + unit_vec*t_int;
if ( X_new < 1 == ones(2,1) ) ...
& ( X_new > 0 == ones(2,1) )
X(:,k) = X_new;
end
end
end
D = sq_distance(X,X);
A = sparse((D < r) - eye(n));
[S,C] = graphconncomp(A);
particles_healed = binornd(ones(length(I),1),p_heal);
still_infected = find(particles_healed == 0);
I = I(still_infected);
numhealed = numhealed + sum(particles_healed);
I_new = I;
% compnum = zeros(length(I),1);
for i = 1:length(I)
compnum = C(I(i));
I_new = union(I_new,find(C == compnum));
end
I = I_new;
if time >= T_plot*TIME
gplot(A,X')
hold on
plot(X(1,I),X(2,I),'r.')
hold off
title(sprintf('time = %1g',time))
axis([0,1,0,1])
% fprintf('number healed = %1g\n',numhealed)
numhealed = 0;
F(TIME) = getframe;
TIME = TIME + 1;
end
end
toc

Reading binary magnetic field with Matlab

I am trying to read some magnetic fields using Matlab. To do so, I am using Windows XP OS and I am sending the command to continuos reading from the device using Putty. While Putty its running and saving the log file I am reading this log file on Matlab. The Device is a HMR2300.
When I am reading this using ASCII format I am not facing any problems at all. However, I need to collect this data in binary and this is the problem.
The output string is 7 bytes long "Xh|Xl|Yh|Yl|Zh|Zl|" Xh = Signed Byte, X axis - Xl= Low byte, X axis.
Since I have four devices, I am collecting all data inside of a while loop with this code:
%*****% Reading from Device 01
[D1, N] = fread(FidMag1, 7);
while (true)
MagData1 = [MagData1, D1'];
CRLF1 = find(MagData1 == 13);
CRLF1 = [0, CRLF1]';
if (CRLF1(end) ~= 7)
MagData1(1:CRLF1(end))=[];
[D1, N] = fread(FidMag1, 7);
else
break;
end
end
ttt = clock();
TimeStamp1(Count) = ttt(6) + ttt(5)*60 + ttt(4)*3600;
NoCalMagX1(Count) = int16(typecast(uint8(MagData1(1)), 'int8')) * int16(256) + (MagData1(2));
NoCalMagY1(Count) = int16(typecast(uint8(MagData1(3)), 'int8')) * int16(256) + (MagData1(4));
NoCalMagZ1(Count) = int16(typecast(uint8(MagData1(5)), 'int8')) * int16(256) + (MagData1(6));
MagData1(1:CRLF1(end))=[];
So, after that I am reading in a row devices 2, 3, and 4.
This is my result:
Image Link
You can see roughly that before and after 200 sec I am having a delay to collect the data and this is what I need to solve. Usually when my field starts to change, I start to receive a delay and I don't know how to solve it.
I've tried to do a different approach with my code however this is just worst then the first one:
%*****% Reading from Device 01
if (SYNC_1)
[D1, countB] = fread(FidMag1, 7-BIB_1);
if (countB ~= 7)
for a=1:countB
temp(BIB_1+a) = D1(a);
end
end
if (flag_temp)
flag_temp = 0;
else if(find(D1 == 13) == 7)
for a=1:countB
temp(a) = D1(a);
end
end
end
BIB_1 = BIB_1 + countB;
if(BIB_1 == 7)
if (find(temp == 13) == 7)
ttt = clock();
TimeStamp1(Count_1) = ttt(6) + ttt(5)*60 + ttt(4)*3600;
NoCalMagX1(Count_1) = int16(typecast(uint8(temp(1)), 'int8')) * int16(256) + (temp(2));
NoCalMagY1(Count_1) = int16(typecast(uint8(temp(3)), 'int8')) * int16(256) + (temp(4));
NoCalMagZ1(Count_1) = int16(typecast(uint8(temp(5)), 'int8')) * int16(256) + (temp(6));
fprintf('SENSOR B\nX:%.0f Y:%.0f Z:%.0f\n\n', NoCalMagX1(Count_1), NoCalMagY1(Count_1), NoCalMagZ1(Count_1));
Count_1 = Count_1 + 1;
else
SYNC_1 = 0;
end
BIB_1 = 0;
end
end
if (~SYNC_1)
[D1, countB] = fread(FidMag1, 7-BIB_1);
BIB_1 = BIB_1 + countB;
if (BIB_1 > 0)
index = find (D1 == 13);
if(index)
BIB_1 = 7 - index(end);
if(BIB_1 == 0)
flag_temp = 1;
for a=(BIB_1+1):7
temp(a-BIB_1) = D1(a);
end
else
for a=(index+1):countB
temp(aux) = D1(a);
aux = aux + 1;
end
end
aux = 1; %temp index
SYNC_1 = 1;
else
SYNC_1 = 0;
BIB_1 = 0;
end
end
end
Results:
Image Link
Do you guys have any idea how can I solve this? To collect data using ascii my approach its similar and I don't have any problems. Also when I am reading from just one device using the first code I don't have any problems too. I am just lost in how to solve that.

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.

Filter points using hist in matlab

I have a vector. I want to remove outliers. I got bin and no of values in that bin. I want to remove all points based on the number of elements in each bin.
Data:
d1 =[
360.471912914169
505.084636471948
514.39429429184
505.285068055647
536.321181755858
503.025854206322
534.304229816684
393.387035881967
396.497969729985
520.592172434431
421.284713703215
420.401106087984
537.05330275495
396.715779872694
514.39429429184
404.442344469518
476.846474245118
599.020867750031
429.163139144079
514.941744277933
445.426761656729
531.013596812737
374.977332648255
364.660115724218
538.306752697753
519.042387479096
1412.54699036882
405.571202133485
516.606049132218
2289.49623498271
378.228766753667
504.730621222846
358.715764917016
462.339366699398
512.429858614816
394.778786157514
366
498.760463549388
366.552861126468
355.37022947906
358.308526273099
376.745272034036
366.934599077274
536.0901883079
483.01740134285
508.975480745389
365.629593988233
536.368800360349
557.024236456548
366.776498701866
501.007025898839
330.686029339009
508.395475983019
429.563732174866
2224.68806802212
534.655786464525
518.711297351426
534.304229816684
514.941744277933
420.32368479542
367.129404978681
525.626188464768
388.329756778952
1251.30895065927
525.626188464768
412.313764019587
513.697381733643
506.675438520558
1517.71183364959
550.276294237722
543.359917550053
500.639590923451
395.129864728041];
Histogram computation:
[nelements,centers] = hist(d1);
nelements=55 13 0 0 1 1 1 0 0 2
I want to remove all points apearing less than 5 (in nelements). It means only first 2 elements in nelements( 55, 13 ) remains.
Is there any function in matlab.
You can do it along these lines:
threshold = 5;
bin_halfwidth = (centers(2)-centers(1))/2;
keep = ~any(abs(bsxfun(#minus, d1, centers(nelements<threshold))) < bin_halfwidth , 2);
d1_keep = d1(keep);
Does this do what you want?
binwidth = centers(2)-centers(1);
centersOfRemainingBins = centers(nelements>5);
remainingvals = false(length(d1),1);
for ii = 1:length(centersOfRemainingBins )
remainingvals = remainingvals | (d1>centersOfRemainingBins (ii)-binwidth/2 & d1<centersOfRemainingBins (ii)+binwidth/2);
end
d_out = d1(remainingvals);
I don't know Matlab function for this problem, but I think, that function with follow code is what are you looking for:
sizeData = size(data);
function filter_hist = filter_hist(data, binCountRemove)
if or(max(sizeData) == 0, binCountRemove < 1)
disp('Error input!');
filter_hist = [];
return;
end
[n, c] = hist(data);
sizeN = size(n);
intervalSize = c(2) - c(1);
if sizeData(1) > sizeData(2)
temp = transpose(data);
else
temp = data;
end
for i = 1:1:max(sizeN)
if n(i) < binCountRemove
a = c(i) - intervalSize / 2;
b = c(i) + intervalSize / 2;
sizeTemp = size(temp);
removeInds = [];
k = 0;
for j = 1:1:max(sizeTemp)
if and(temp(j) > a, less_equal(temp(j), b) == 1)
k = k + 1;
removeInds(k) = j;
end
end
temp(removeInds) = [];
end
end
filter_hist = transpose(temp);
%Determines when 'a' less or equal to 'b' by accuracy
function less_equal = less_equal(a, b)
delta = 10^-6; %Accuracy
if a < b
less_equal = 1;
return;
end
if abs(b - a) < delta
less_equal = 1;
return;
end
less_equal = 0;
You can do something like this
nelements=nelements((nelements >5))

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?)