# [newbie] I was given a code with vectors/matrices and don't exactly know the meaning of vars/parameters

1 view (last 30 days)
Adam Zielinski on 31 Jul 2021
Commented: Image Analyst on 31 Jul 2021
Hi,
I'm totally new in MatLab but I really do try to understand it. Before I decided to write this question, I have searched the internet but not being a native English speaker I'm not sure what net answer could be valuable to me. So please don't be angry at me and please help to understand those basics. This is my almost first MatLab code I can see with my own eyes and I'm a bit lost, I must admit.
It's all about vector quantization, the Lloyd's algorithm. Another part of code (not presented here) reads a b/w picture and gets blocks of axb pixels from it. The code I was given is presented below, and my questions signed "???" next to place I don't get.
I would be grateful for explanation...
function codeb_new=lloyd(codeb_start,bloki)
% Lloyd's algorithm
niter=15;
codeb_start=0;
bloki=64; % number of blocks I guess
[a,b,nvec]=size(codeb_start); % ??? what is nvec, couldn't find exact meaning (new vector? number of vectors?)
[aa,bb,LL]=size(bloki); % ??? why aa, bb are doubled? MatLab's suggestion is they are not in use
codeb_new=zeros(a,b,nvec);
count=zeros(nvec,1);
voron=zeros(size(codeb_start));
codeb=codeb_start;
for iter=1:niter
error_new=0;
% quantization
for iblok=1:LL
blok=bloki(:,:,iblok); % ??? why ":" in two first positions?
err=10^10;
ivecopt=0;
for ivec=1:nvec
vec=codeb(:,:,ivec);
diff=blok-vec;
er=sum(sum(diff.^2));
if er < err
err=er;
ivecopt=ivec;
end
end
count(ivecopt)=count(ivecopt)+1;
voron(:,:,ivecopt)= voron(:,:,ivecopt)+bloki(:,:,iblok);
error_new=error_new+err;
end
iter % ??? why "iter" is alone here? Is it just to display number of iterations done?
error_new % ??? same as above - is it just to display the current state of this variable?
% new codebook
for ivec=1:nvec
if count(ivec) > 0
codeb_new(:,:,ivec)=voron(:,:,ivec)/count(ivec);
else
codeb_new(:,:,ivec)=codeb(:,:,ivec);
end
end
% distance to next codebook
dcodeb=0;
for ivec=1:nvec
delt=codeb_new(:,:,ivec)-codeb(:,:,ivec);
dcodeb=dcodeb+sum(sum(delt.^2));
end
dcodeb
if dcodeb < 0.1
iter
break
else
codeb=codeb_new; % ??? is this a value assignment of "codeb_new" function result?
end
end
end

DGM on 31 Jul 2021
Just looking at the question lines:
%[a,b,nvec]=size(codeb_start); % ??? what is nvec, couldn't find exact meaning (new vector? number of vectors?)
size() supports syntax for either single or multiple output arguments. In this case, codeb_start is presumed to be a 3 (or less) dimensional array. Consider the example:
A = rand(5,10,15);
size(A) % everything in one vector
ans = 1×3
5 10 15
[rows,cols,pages] = size(A) % each dimension size as a scalar
rows = 5
cols = 10
pages = 15
So in this case, nvec is confusingly the number of 2D pages in a 3D array.
%[aa,bb,LL]=size(bloki); % ??? why aa, bb are doubled? MatLab's suggestion is they are not in use
aa and bb are just different variable names since a and b are already in use. There is no implicit multiplication or anything going on there. As you say, it doesn't appear that they are used for anything.
%blok=bloki(:,:,iblok); % ??? why ":" in two first positions?
The use of : in a comma-separated list like this is basically the same as
thispage = A(1:end,1:end,pageindex);
In this case, it selects all rows and all columns and a specified page index. The result is the extraction of a 2D array from a 3D array.
%iter % ??? why "iter" is alone here? Is it just to display number of iterations done?
%error_new % ??? same as above - is it just to display the current state of this variable?
Yep. That's just there for display.
%codeb=codeb_new; % ??? is this a value assignment of "codeb_new" function result?
codeb_new is the output. As this appears to be an iterative process, codeb_new is assigned back to codeb for the next iteration. Note that when the condition is satisfied in that if statement, the loop breaks and codeb_new will simply be returned by the function.
There is another issue that's probably worth pointing out:
function codeb_new=lloyd(codeb_start,bloki) % codeb_start and bloki are input args
% ...
codeb_start=0; % but they both get redefined here
bloki=64;
These two lines overwrite any input arguments that may be provided when the function lloyd() is called. It's implied that bloki is a 3D array, so assigning a scalar to it probably breaks everything.
Image Analyst on 31 Jul 2021
Don't feel bad @Adam Zielinski, that is terrible code. I'd give the author a C minus or D on their code writing abilities. Ignoring input variables, terrible non-descriptive name choices for variables, far too few comments, etc. It's just plain bad. Hopefully DGM's thorough explanations help over come some of that.

R2020a

### Community Treasure Hunt

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

Start Hunting!