How Can I replace function 'strcat'
Mostrar comentarios más antiguos
How can I replace 'strcat' function in the code below Manually so that it will work same as the function and the output of the code will be the same.
thanks
prob_dist = [32 17 9 9 18 42 10 5 1 2 3 21 23 13 13 26 56 16 15 14 7 4 3];
sorted_prob = prob_dist;
rear = 1;
y=length(prob_dist);
while (length(sorted_prob) > 1)
% Sort probs
%[sorted_prob,indeces] = sort(sorted_prob,'ascend');
initialFrequencyList = sorted_prob
% clear initialFrequencyIndex
initialFrequencyIndex = 1:length(sorted_prob)
N = length(initialFrequencyList);
for nodes = 1:N
for nodes2 = (nodes + 1):N
if initialFrequencyList(nodes) > initialFrequencyList(nodes2)
temporaryFrequency = initialFrequencyList(nodes);
initialFrequencyList(nodes) = initialFrequencyList(nodes2);
initialFrequencyList(nodes2) = temporaryFrequency;
temporaryIndex = initialFrequencyIndex(nodes);
initialFrequencyIndex(nodes) = initialFrequencyIndex(nodes2);
initialFrequencyIndex(nodes2) = temporaryIndex;
end
end
end
sorted_prob = initialFrequencyList
indeces = initialFrequencyIndex
% Sort string based on indeces
sorted_str = sorted_str(indeces);
% Create new symbol
new_node = strcat(sorted_str(2),sorted_str(1)); %strcat
new_prob = sum(sorted_prob(1:2));
% Dequeue used symbols from "old" queue
sorted_str = sorted_str(3:length(sorted_str));
sorted_prob = sorted_prob(3:length(sorted_prob));
% Add new symbol back to "old" queue
sorted_str = [sorted_str, new_node];
sorted_prob = [sorted_prob, new_prob];
% Add new symbol to "new" queue
newq_str(rear) = new_node; %no square bracket
newq_prob(rear) = new_prob;
rear = rear + 1;
end
indeces
%%Form Huffman Tree Data
% Get all elements for the tree (symbols and probabilities) by concatenating
% the original set of symbols with the new combined symbols and probabilities
tree = [newq_str,init_str];
tree_prob = [newq_prob, init_prob];
% Sort all tree elements
[sorted_tree_prob,indeces] = sort(tree_prob,'descend');
sorted_tree = tree(indeces);
%%Calculate Tree Parameters
% Calculate parent relationships for all tree elements.
% This will allow the treeplot command to realize the correct tree structure.
parent(1) = 0;
num_children = 2;
for i = 2:length(sorted_tree)
% Extract my symbol
me = sorted_tree{i};
% Find my parent's symbol (search until shortest match is found)
count = 1;
parent_maybe = sorted_tree{i-count};
diff = strfind(parent_maybe,me); %strfind
while (isempty(diff))
count = count + 1;
parent_maybe = sorted_tree{i-count};
diff = strfind(parent_maybe,me);%strfind
end
parent(i) = i - count;
end
3 comentarios
Jiro Doke
el 27 de Mzo. de 2011
Can you explain why you need to do this?
Tadele Gerem
el 29 de Mzo. de 2011
Walter Roberson
el 29 de Mzo. de 2011
So what happened when you converted the code to use [] like I showed in the other discussion?
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!