Reputation: 29
My teacher asked us to write a program for Huffmam Coding in MATLAB, but I met a problem:
Undefined function 'lt' for input arguments of type 'cell'.
Error in work5455 (line 49)
Which is this line:
if(cell{k+1}(1)<cell{k}(1)&&cell{k}(2)==-1)
Since there is no 'lt',how can I solve it? My teacher is just too busy to answer my e-mail..... Here`s the code(not finished),thank you very much!
fprintf('Reading data...')
data=imread('C:\Users\dell\Desktop\2.png');
data=rgb2gray(data);
data=uint8(data);
fprintf('Done!\n')
if~isa(data,'uint8'),
error('input argument must be a uint8 vector')
end
f=repmat(0,1,256);
len=length(data);
for j=0:255
for i=1:len
if data(i)==j;
f(j)=f(j)+1;
end
end
end
f=double(f./len);
simbols=find(f~=0);
f=f(simbols);
[f,sortindex]=sort(f)
simbols=simbols(sortindex)
len=length(simbols);
codeword=cell(1,len);
huffnode=cell(1,2*len);
for i=1:len
cell{i}={f(i),-1,-1,-1};
end
for i=len+1:2*len-1
cell{i}={-1,-1,-1,-1};
end
m=len
%for i=1:len
for j=1:len+i-1
m=m-1;
for k=1:m
if(cell{k+1}(1)<cell{k}(1)&&cell{k}(2)==-1)
cell{2*len}=cell{k+1};
cell{k+1}=cell{k};
cell{k}=cell{2*len};
end
end
end
Upvotes: 1
Views: 2220
Reputation: 1481
The Output of cell{k+1}(1) is a 1x1 cell. Since you cannot compare cells to other cells, you have to make a datatype conversion using cell2mat
to get the number itself before. Using
if(cell2mat(cell{k+1}(1))<cell2mat(cell{k}(1))&&cell2mat(cell{k}(2))==-1)
should solve your Problem.
Since cell is a built-in function in matlab I would highly recommend renaming your variable. As @AndrasDeak stated in the comments you might get Problems later on otherwise. In General, you should never use built-in function-names as your variable- or function-names. Please note that i
and j
represent the imaginary unit in matlab and thus should not be used as Iteration variables either.
Upvotes: 3