📄 zhang.m
字号:
function varargout = zhang(varargin)
% ZHANG M-file for zhang.fig
% ZHANG, by itself, creates a new ZHANG or raises the existing
% singleton*.
%
% H = ZHANG returns the handle to a new ZHANG or the handle to
% the existing singleton*.
%
% ZHANG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ZHANG.M with the given input arguments.
%
% ZHANG('Property','Value',...) creates a new ZHANG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before zhang_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to zhang_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help zhang
% Last Modified by GUIDE v2.5 25-Mar-2002 12:19:15
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @zhang_OpeningFcn, ...
'gui_OutputFcn', @zhang_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before zhang is made visible.
function zhang_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to zhang (see VARARGIN)
% Choose default command line output for zhang
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% This sets up the initial plot - only do when we are invisible
% so window can get raised using zhang.
if strcmp(get(hObject,'Visible'),'off')
axis([0 1 0 1.5]);
end
% UIWAIT makes zhang wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = zhang_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear;
clc;
cla;
in=inputdlg('请输入概率矩阵,如!![0.2 0.15 0.13 0.12 0.1 0.09 0.08 0.07 0.06]','the input dlg',1);
out=str2mat(in);
n2=str2num(out);
[m n]=size(n2);
amount=sum(n2);
if amount~=1
errordlg('输入的概率 和不为1 或为空','·#error dlg','off');
else
m=2*n-1;
a=zeros(1,n);
hftree=struct('weight','parent','lch','rch');
codetype=struct('bit','start');
for i=1:m
hftree(i).parent=0;
hftree(i).lch=0;
hftree(i).rch=0;
hftree(i).weight=0;
end
for i=1:(n+1)
codetype(i).bit=a;
codetype(i).start=0;
end;
for i=1:n
hftree(i).weight=n2(i);
end;
for i=(n+1):m
p1=0;
p2=0;
s1=2;
s2=2;
for j=1:(i-1)
if hftree(j).parent==0&hftree(j).weight<s1
s2=s1;
s1=hftree(j).weight;
p2=p1;
p1=j;
elseif hftree(j).parent==0&hftree(j).weight<s2
s2=hftree(j).weight;
p2=j;
end;
end;
hftree(p1).parent=i;
hftree(p2).parent=i;
hftree(i).lch=p1;
hftree(i).rch=p2;
hftree(i).weight=hftree(p1).weight+hftree(p2).weight;
end;
for i=1:n
codetype(n+1).start=n+1;
c=i;
p=hftree(i).parent;
while p>0
codetype(n+1).start=codetype(n+1).start-1;
if hftree(p).lch==c
codetype(n+1).bit(codetype(n+1).start)=0;
else
codetype(n+1).bit(codetype(n+1).start)=1;
end;
c=p;
p=hftree(p).parent;
end;
codetype(i).start=codetype(n+1).start;
codetype(i).bit=codetype(n+1).bit;
end;
for i=1:n
a=sprintf('第%d个概率的值为:%f',i,hftree(i).weight);
disp(a);
arry=[];
r=1;
for j=(codetype(i).start):n
arry(r)=codetype(i).bit(j);
r=r+1;
end;
a=sprintf('第%d个概率的哈夫曼编码为:',i);
disp(a);
disp(arry);
end;
averagelength=0.00;
informationvalue=0.00;
for i=1:n
averagelength=hftree(i).weight*(n+1-codetype(i).start)+averagelength;
informationvalue=informationvalue-hftree(i).weight*log2(hftree(i).weight);
end;
a=sprintf('huffman average length of the code is :%f',averagelength);
disp(a);
a=sprintf('the information of the information source is :%f',informationvalue);
disp(a);
a=sprintf('the code effience of the method huffman is :%f',informationvalue/averagelength);
disp(a);
for i=1:n
x0=0.5;
y0=(n+2)/(n+1);
dy=2/(n+1);
s=1;
for j=(codetype(i).start):n
axis([0 1 0 1.5]);
hold on;
dx=1/(2^(s+1)+2);
if codetype(i).bit(j)==0
x=(x0-dx):1/100:x0;
t=(x0-dx):dx:x0;
y=(x-x0)*dy/dx+y0;
y1=(t-x0)*dy/dx+y0;
plot(x,y,'r');
hold on;
scatter(t,y1,'filled');
hold on;
F(i)=getframe;
pause(0.1);
x0=x0-dx;
s=s+1;
else x=x0:1/100:(x0+dx);
t=x0:dx:(x0+dx);
y=(x0-x)*dy/dx+y0;
y1=(x0-t)*dy/dx+y0;
plot(x,y,'g');
hold on;
scatter(t,y1,'filled');
hold on;
F(i)=getframe;
pause(0.1);
x0=x0+dx;
s=s+1;
end;
y0=y0-dy;
end;
end;
axis([0 1 0 1.5]);
movie(F,1);
%[0.2 0.15 0.13 0.12 0.1 0.09 0.08 0.07 0.06]
end;
axes(handles.axes1);
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
case 1
plot(rand(5));
case 2
plot(sin(1:0.01:25));
case 3
comet(cos(1:.01:10));
case 4
bar(1:10);
case 5
plot(membrane);
case 6
surf(peaks);
end
% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject handle to FileMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to OpenMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
open(file);
end
% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to PrintMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)
% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to CloseMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
['Close ' get(handles.figure1,'Name') '...'],...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end
delete(handles.figure1)
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'comet(cos(1:.01:10))', 'bar(1:10)', 'plot(membrane)', 'surf(peaks)'});
% --- Executes on selection change in popupmenu3.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -