In an assignment A(:) = B, the number of elements in A and B must be the same.

1 view (last 30 days)
I have a problem in the line with <<<<<<<<<<<<<<<
TestID= fopen('Test_text_file.txt','r');
abstract_words= textscan(TestID , '%s');
fclose(TestID);
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' ...
'(',')','.',',','/','-',' '].';
num_words= length(abstract_words{1});
Prob=zeros(length(alphabet),1);
R= reshape(abstract_words{1},[1,num_words]);
C2M= cell2mat(R);
for i= 1:length(alphabet)
F= find(C2M == alphabet(i));
Prob(i) = length(F);
display(strcat(alphabet(i),' --> ',num2str(Prob(i))));
end
space= num_words-1;
Prob(33)=space;
Prob= Prob /sum(Prob);
figure;
stem(Prob);
[J,I] = sort(Prob, 'descend'); %sort probabilities descending with indicating their indices
SA= alphabet(I); %sorted alphabet
LengthOfAlphabet= i;
for i=1:LengthOfAlphabet
Code(i)= ' ';
index{i} = i;
end
while(length(Prob)> 2)
[~,Prob] = sort(Prob, 'descend');
Smallest= Prob(length(Prob));
Before_smallest = Prob(length(Prob)-1);
Smallest_set = index{Smallest};
BS_set = index{Before_smallest};
Smallest_P = Prob(Smallest);
BF_P = Prob(Before_smallest);
Joined_set = {Smallest_set,BS_set};
SumProb= Smallest_P + BF_P ;
% Last =index(length(Prob));
% BL = index(length(Prob)-1);
index{Prob(length(Prob))} = ' ';
index{Prob(length(Prob)-1)} = ' ';
Prob (Prob(length(Prob):length(Prob)-1))= ' ';
index = [index Joined_set];
Prob = Prob+SumProb;
for o=1:length(Smallest_set)
Code(Smallest_set(o))= strcat('1',Code(Smallest_set(o)));
end
for o=1:length(BS_set)
Code(BS_set(o))= strcat('0',Code(BS_set(o))); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
end
% Prob=Prob-1;
end
Symbols=alphabet;
Code_Word=Code;
  3 Comments
Bob Thompson
Bob Thompson on 27 Mar 2018
I would guess you're getting the error because '0' and Code(BS_set(o)) are not the same size. I would assume that Code(BS_set(o)) is a matrix, so matlab isn't going to like doing strcat() between a matrix and a string.
I would suggest confirming that all of your variables are the right class type, you can check with class(Code(BS_set(o))). If they appear correct try running the command in the command line, rather than in the script itself. This will tell you if there is an issue with the command and entered values, or something in the script itself.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 27 Mar 2018
I would have thought you'd get an error in the previous loop with your:
Code(Smallest_set(o)) = strcat('1',Code(Smallest_set(o)));
Code(Smallest_set(o)) selects a given number (let's call it n) of elements from Code. You prepend that n number of elements with a '1', hence you now have something with n+1 elements. Trying to stuff these n+1 elements into the original n elements is never going to work and that is what the error message it telling you.
How to solve this, I do not know since I have no idea what it is you're trying to do.
  1 Comment
Fatma Alsadani
Fatma Alsadani on 27 Mar 2018
I'm trying te replace an alphabet with a set of 1's and z's for each alphabet based on huffman theory

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!