实现有限长序列的基本运算(包括:加法、乘法、累加、移位、翻褶、抽取、插值、卷 积和),并以 GUI 的形式将这些运算整合起来,使用者可通过向 GUI 输入任意有限长序列得 到对应的运算结果。
-
加法:对两个序列中对应位置的元素进行相加,得到一个新的序列,要求两个序列的长度相同。
-
乘法:对两个序列中对应位置的元素进行相乘,得到一个新的序列,要求两个序列的长度相同。
-
累加:对序列中的元素进行累加操作,即将每个元素与其前面所有元素的和依次相加,得到一个新的序列。
-
移位:将序列中的元素按照指定的步长向左或向右移动,空出的位置用零或者其他指定的值填充。
-
翻褶:将序列中的元素顺序完全颠倒,即首尾对调。
-
抽取:从序列中按照指定的步长抽取元素,得到一个新的序列。
-
插值:在序列中插入新的元素,通常是在指定位置插入一个特定的值或者另一个序列。
-
卷积:对两个序列进行卷积操作,得到一个新的序列,常用于信号处理和图像处理中。
题目分析:
该题是对序列的各种数据操作,可根据“需要两个序列”还是“需要一个序列”可分为两类操作,第一类操作为“加法、乘法、卷积和”,需要接收两个序列,第二类操作为“累加、移位、翻褶、抽取、插值”。基本定义如下:
设计思路:
第一类:通过两个可编辑文本框获取两个序列,运用axes()与stem()将A和B两个序列以针状图的形式画在轴上,接下来在按钮中写回调函数,按下后判断文本框中序列元素个数是否相等,不相等则弹出警告,相等则继续进行操作。
加法和乘法直接用A+B和A.*B即可得到,卷积和运用conv()即可操作完毕;
第二类:累加,运用for循环或者cumsum函数即可完成功能。移位,运用randn生成一个移位的随机数,通过if判断随机数的正负来决定左移还是右移,然后通过矩阵合并一个零矩阵来实现矩阵的移位。翻褶,通过以y轴为对称轴左右翻转得到,可运用fliplr函数来进行该操作。抽取,接收抽取的n值后,运用for循环找符合要求的矩阵索引来合成一个新矩阵,即完成抽取的功能。插值,接收插入的n值后,新建一个空矩阵,运用一个for循环不断地将零向量、所求矩阵索引与该矩阵合并,最终得到符合要求的新矩阵。
代码
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 09-Mar-2024 14:45:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- 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)
eval(“A=”+get(handles.edit1,'String'));
eval(“B=”+get(handles.edit2,'String'));
x=0:length(A)-1;
axes(handles.axes1);
stem(x,A);
axes(handles.axes2);
stem(x,B);
V=get(handles.popupmenu1,'Value');
if numel(A)==numel(B)
switch V
case 1
C1=A+B;
axes(handles.axes3);
set(handles.text20,'String',num2str(C1));
stem(x,C1)
case 2
axes(handles.axes3);
C2=A.*B;
stem(C2)
set(handles.text20,'String',num2str(C2));
case 3
axes(handles.axes3);
C3=conv(A,B);
stem(C3);
set(handles.text20,'String',num2str(C3))
end
else
msgbox('¶þÕßÔªËظöÊý²»Í¬','error','modal');
end
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
A2=str2num(get(handles.edit3,'String'));
x=0:1:length(A2)-1;
axes(handles.axes4);
stem(x,A2);
V=get(handles.popupmenu2,'Value');
switch V
case 1
C1=cumsum(A2);
axes(handles.axes6);
stem(x,C1);
set(handles.text17,'String',num2str(C1))
case 2
axes(handles.axes6);
r=round(randn()*10)
if r>0
x=zeros(1,r);
C2=[x,A2];
else
r=-r;
x=zeros(1,r);
C2=[A2,x];
end
z=0:1:length(C2)-1;
stem(z,C2);
set(handles.text17,'String',num2str(A2))
case 3
axes(handles.axes6);
x=0:1:length(A2)-1;
x=-x;
stem(x,A2);
set(handles.text17,'String',num2str(fliplr(A2)))
case 4
ninput=inputdlg('ÇëÊäÈë³éÈ¡µÄ¼ä¸ôÊý');
n=str2double(ninput);
cqu=[];
for i=1:n:length(A2)
cqu=[cqu,A2(i)];
end
axes(handles.axes6);
x=0:1:length(cqu)-1;
stem(x,cqu);
set(handles.text17,'String',num2str(cqu))
case 5
axes(handles.axes6)
ninput=inputdlg('ÿÁ½µã¼ä²åÈë¶àÉÙ¸öÖµ')
n=str2double(ninput);
C5=A2(1);
for i=2:1:length(A2)
z=zeros(1,n);
C5=[C5,z,A2(i)]
end
x=0:1:length(C5)-1;
stem(x,C5);
set(handles.text17,'String',num2str(C5));
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (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.
标签:String,see,get,卷积,handles,hObject,源码,MATLAB,eventdata
From: https://blog.csdn.net/mohen_777/article/details/139766089