How can I make a function from this few lines of codes?

1 次查看(过去 30 天)
Hi all, can anyone please tell me how can I make function of this calculation so that I can perform the same operation for any given range of A?
clear all; close all; clc;
A = -40:2:40;
B = A(find(A>0))./max(A);
C = -A(find(A<=0))./min(A);
D = [C,B];
figure(1),
subplot(2,1,1)
plot(1:numel(A),A);
subplot(2,1,2)
plot(1:numel(D),D)

回答(1 个)

Voss
Voss 2023-2-17
% calling the function defined below
filter_and_plot(-40:2:40)
% defining the function
function filter_and_plot(A)
B = A(find(A>0))./max(A);
C = -A(find(A<=0))./min(A);
D = [C,B];
figure()%(1),
subplot(2,1,1)
plot(1:numel(A),A);
subplot(2,1,2)
plot(1:numel(D),D)
end

Community Treasure Hunt

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

Start Hunting!

Translated by