Vous devez mettre ce corrigé dans un ficher et la fonction POSNEG dans un autre ficher de fonction
% This function outputs the correction of serie 2.
% Execute "corrige" in the command line
%
function corrige
disp(' ');
disp('Corrigé Série 2');
disp('================');
disp(' ');
%--------------------------------------------------------------------------
disp('Exercice 1:');
a = 2
b = 2
disp('a) a >= (b-1)');
a >= (b-1)
disp('b) ~(a < b)');
~(a < b)
disp('c) (a > b) || (a < b)');
(a > b) || (a < b)
disp('d) (b >= a) && (a ~= b)');
(b >= a) && (a ~= b)
disp('e) xor((a > b), (a == b))');
xor((a > b), (a == b))
%--------------------------------------------------------------------------
disp('Exercice 2:');
N = 3;
for i = 1:N
for j = 1:N
a(i,j) = j + (N * (i - 1));
end
end
a
%--------------------------------------------------------------------------
disp('Exercice 3:');
disp('a) boucle for :');
disp(' ');
disp(' product = 1;');
disp(' for i = 1:16');
disp(' if (mod(i, 2) == 0)');
disp(' product = product * i;');
disp(' end');
disp(' end');
product = 1;
for i = 1:16
if (mod(i,2) == 0)
product = product * i;
end
end
product
disp('b) boucle while :');
disp(' ');
disp(' product = 1;');
disp(' i = 2;');
disp(' while (i <= 16)');
disp(' product = product * i;');
disp(' i = i + 2;');
disp(' end');
product = 1;
i = 2;
while (i <= 16)
product = product * i;
i = i + 2;
end
product
disp('c) fonction prod(v) :');
disp(' ');
disp(' v = [2:2:16];');
disp(' prod(v)');
v = [2:2:16];
prod(v)
%--------------------------------------------------------------------------
disp('Exercice 4:');
disp('function [ pos, neg ] = posneg( a )');
disp('');
disp('for i=1:3');
disp(' for j=1:3');
disp(' if(a(i,j)>=0)');
disp(' pos(i,j) = a(i,j);');
disp(' neg(i,j) = 0;');
disp(' else');
disp(' pos(i,j) = 0;');
disp(' neg(i,j) = a(i,j);');
disp(' end');
disp(' end');
disp('end');
a = [-1,2,-3;4,-5,6;-7,8,9]
disp('[x,y] = posneg(a)');
[x,y] = posneg(a)
end
% la fonction POSNEG
function [pos,neg]=POSNEG(a)
for i=1:3
for j=1:3
if(a(i,j)>=0)
pos(i,j) = a(i,j);
neg(i,j) = 0;
else
pos(i,j) = 0;
neg(i,j) = a(i,j);
end
end
end