为了教育目的,我试图在MATLAB中实现一个stem和绘图算法。在我发布代码之前,让我介绍一下我的方法的步骤。让我们考虑一下,我们有两个数字:
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 6stems = 2再次出现5次,因此再次:
leaf(1:5)
ans =
0 1 2 3 4
leaf(1:5)=[]
leaf =
6对于stems = 5,我们有1片叶子
leaf(1)
ans =
6为此,我使用了一个map容器,并创建了以下代码:
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怎么啦?
发布于 2017-01-31 21:16:11
在@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 =
6https://stackoverflow.com/questions/41967064
复制相似问题