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 Kommentare

Jiro Doke
Jiro Doke am 27 Mär. 2011
Can you explain why you need to do this?
Tadele Gerem
Tadele Gerem am 29 Mär. 2011
Dear Jiro,
I want to change the matlab code to VHDL using accelDSP but 'strcat' is not synthesizable in accelDSP.
Walter Roberson
Walter Roberson am 29 Mär. 2011
So what happened when you converted the code to use [] like I showed in the other discussion?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Mär. 2011

0 Stimmen

You would do it the same way as we discussed before.

16 Kommentare

Tadele Gerem
Tadele Gerem am 30 Mär. 2011
Dear Walter,
yes I have tried as we discussed before and two of your suggestions work perfectly.
strcat(w,code{i}) ---> [w, code{i}];
and display([my_str(i), ' --> ', num2str(prob_dist(i))]);
works perfectly but the rest are not working .
and Now I am left with one 'strcat' and two 'strfind' functions
I have changed the sort function with equivalent coding of Matlab
.And pls help me for that.
thanks
Walter Roberson
Walter Roberson am 30 Mär. 2011
You did not indicate the data type of sorted_str . If it is a cell array, then
new_node = strcat(sorted_str(2),sorted_str(1))
would become
new_node = [sorted_str{2},sorted_str{1}];
Tadele Gerem
Tadele Gerem am 30 Mär. 2011
Dear Walter ,
the sample output for sorted_str is
sorted_str =
Columns 1 through 22
'z' '.' 'q' 'p' 'b' 'y' 'g' 'h' 'u' 'm' 'l' 't' 'd' 'i' 'c' 'a' 'f' 'n' 'o' 'r' 's' 'e'
Column 23
' '
and I replace
new_node = strcat(sorted_str(2),sorted_str(1))
with
new_node = [sorted_str{2},sorted_str{1}];
and the error is
??? Subscripted assignment dimension mismatch.
Error in ==> huffmanFunction at 118
newq_str(rear) = new_node; %no square bracket
Walter Roberson
Walter Roberson am 30 Mär. 2011
That's a cell array, alright. Okay, I should have said,
new_node = {[sorted_str{2},sorted_str{1}]};
Tadele Gerem
Tadele Gerem am 30 Mär. 2011
It works perfectly,
And what about the 'strfind' function in the code below pls
count = 1;
parent_maybe = sorted_tree{i-count};
diff = strfind(parent_maybe,me);
while (isempty(diff))
count = count + 1;
parent_maybe = sorted_tree{i-count};
diff = strfind(parent_maybe,me);
end
parent(i) = i - count;
end
thank you so much
Walter Roberson
Walter Roberson am 30 Mär. 2011
That code doesn't look likely to be correct to me, but that's a matter for another day.
For your purposes:
function pos = strfind(string, pattern)
pos = [];
N = length(pattern) - 1;
for K = 1 : length(string) - N
if all(string(K:K+N) == pattern)
pos = K;
return
end
end
end
Tadele Gerem
Tadele Gerem am 30 Mär. 2011
Dear Walter,
I replaced the code you write as the one below but it doesn't work.
What is my problem?
diff=[];
N = length(me) - 1;
for K = 1 : length(parent_maybe) - N;
if all(parent_maybe(K:K+N) == me)
pos =K;
return
end
end
end
thanks a lot
Walter Roberson
Walter Roberson am 30 Mär. 2011
The return call is suitable for a function but not for expanding the code in-line. In-line you would use break instead of return, and you would remove the final "end", and you would assign K to diff instead of to pos.
Tadele Gerem
Tadele Gerem am 30 Mär. 2011
Dear Walter,
The syntax works fine,
pos = [];
N = length(me) - 1;
for diff = 1 : length(parent_maybe) - N
if all(string(diff:diff+N) == me)
pos = diff;
break
end
end
But the output is a little unexpected,
> In huffmanFunction at 161
Warning: string is obsolete and will be discontinued.
Use char instead.
ans =
Columns 1 through 12
'' '0' '00' '000' '0000' '00000' '000000' '0000000' '00000000' '000000000' '0000000000' '00000000000'
Columns 13 through 19
'000000000000' '0000000000000' '00000000000000' '000000000000000' '0000000000000000' '00000000000000000' '000000000000000000'
thanks
Walter Roberson
Walter Roberson am 30 Mär. 2011
It would have been easier if you had simply put my suggest strfind as a function in your file. Anyhow, replace string with parent_maybe (which you had done before and reverted?)
Tadele Gerem
Tadele Gerem am 31 Mär. 2011
Dear Walter ,
Is it possible to replace 'num2str' function with other matlab codes as I found it to be non synthesizable?
Btw,I am very much thankful to your help as it is very valuable to me.
Walter Roberson
Walter Roberson am 31 Mär. 2011
It is probably possible to replace num2str, but I would need to know the datatypes; if you are printing out anything other than integers then I need to know the format you want to use.
Tadele Gerem
Tadele Gerem am 31 Mär. 2011
Dear Walter,
num2str function is found below in this code of my matlab function. And the sample output is shown below the code.
for i = 1:length(sorted_tree)
% Initialize code
code{i} = '';
% Loop until root is found
index = i;
p = parent(index);
while(p ~= 0)
% Turn weight into code symbol
w = num2str(weight(index))
% Concatenate code symbol
code{i} = [w,code{i}]; %%strcat
% Continue towards root
index = parent(index);
p = parent(index);
end
end
codeBook = [sorted_tree', code']
w =
0
w =
1
w =
0
w =
0
w =
1
w =
0
Walter Roberson
Walter Roberson am 1 Apr. 2011
Then the first thing to try is replacing num2str(X) by sprintf('%d',X)
Tadele Gerem
Tadele Gerem am 2 Apr. 2011
Dear Walter,
The function 'sprintf' is also not synthesizable
Walter Roberson
Walter Roberson am 2 Apr. 2011
See this for code:
http://www.mathworks.com/matlabcentral/answers/1804-ascii-string-with-sci-transmit#answer_2706

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by