How Can I replace function 'strcat'
이전 댓글 표시
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
Jiro Doke
2011년 3월 27일
Can you explain why you need to do this?
Tadele Gerem
2011년 3월 29일
Walter Roberson
2011년 3월 29일
So what happened when you converted the code to use [] like I showed in the other discussion?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!