我正在尝试在MATLAB中实现一个词干和绘图算法用于教育目的。在发布我的代码之前,让我先介绍一下我的方法的步骤。让我们考虑一下我们有两位数字:
A=[20 12 13 21 56 13 16 17 22 23 24];
茎可以通过
给出stems=fix(A/10)
stems =
2 1 1 2 5 1 1 1 2 2 2
和叶子可以由
给出leaf=fix(mod(A,10))
leaf =
0 2 3 1 6 3 6 7 2 3 4
我所做的,就是对茎进行分类,并根据那种叶子进行分类:
[stems, index]=sort(stems,'ascend')
leaf=leaf(index)
stems =
1 1 1 1 1 2 2 2 2 2 5
leaf =
2 3 3 6 7 0 1 2 3 4 6
这是基本的想法:
stems
leaf
对每个词干重复此过程,在每个步骤中我缩短leaf
数组。例如,对于stems = 1
,我们有[5 1]
,所以我会有
leaf(1:5)
ans =
2 3 3 6 7
leaf(1:5)=[]
leaf =
0 1 2 3 4 6
stems = 2
再次5次,所以再次:
leaf(1:5)
ans =
0 1 2 3 4
leaf(1:5)=[]
leaf =
6
现在stems = 5
,我们有1片
leaf(1)
ans =
6
为此,我使用了一个地图容器,并创建了以下代码:
function stem_leaf_plot(v)
if ~isnumeric(v) % check that program will accept array as a integers
error( 'Input V must be numeric');
end
stems=fix(v/10);
leaf=fix(rem(v,10));
[stems, index]=sort(stems,'ascend');
leaf=leaf(index);
string_stems=num2str(stems);
%% count occurence of each stem
MAP=containers.Map();
n=length(stems); % total element of stems array
for ii=1:n
if isKey(MAP,string_stems(ii))
MAP(string_stems(ii))= MAP(string_stems(ii))+1;
else
MAP(string_stems(ii))=1;
end
end
MAP_count=length(MAP);
stem=num2str(cell2mat(keys(MAP)));
for jj=1:MAP_count
frequency=(MAP(string_stems(jj)));
fprintf('leafs of stem %d',stem(jj));
disp(leaf(1:frequency));
leaf(1:frequency)=[]; % delete elements step by step
end
end
但是,我的代码的结果是
stem_leaf_plot(A)
leafs of stem 32 2 3 3 6
leafs of stem 49 7 0 1 2 3 4 6
有什么问题?
答案 0 :(得分:0)
在@Adriaan的建议之后,我使用hist
来计算频率,而不是容器。这是我更新的代码:
function stem_leaf_plot(v)
if ~isnumeric(v) % check that program will accept array as a integers
error( 'Input V must be numeric');
end
stems=fix(v/10);
leaf=fix(rem(v,10));
[stems, index]=sort(stems,'ascend');
leaf=leaf(index);
[a,b]=hist(stems,unique(stems));
n=length(a);
for ii=1:n
fprintf('leaf of stem %d is ',b(ii));
leaf(1:a(ii))
leaf(1:a(ii))=[];
end
>> A=[20 12 13 21 56 13 16 17 22 23 24];
>> stem_leaf_plot(A)
leaf of stem 1 is
ans =
2 3 3 6 7
leaf of stem 2 is
ans =
0 1 2 3 4
leaf of stem 5 is
ans =
6