Hello I'm getting this warning "Variable 'x1' might be set by a nonscaler operator".
    7 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    AlireaA Mirrabie
 il 20 Set 2022
  
    
    
    
    
    Commentato: AlireaA Mirrabie
 il 22 Set 2022
            This is a exercise to write a piece of code which would determine and plot the odd and even parts of a discontinuous time signal. It's not my code I just copied it from youtube. I would like to know why this code works just fine with continuous time signals and how can I rewrite the code to avoid this issue.
close; clear; clc;
n = -5:5;
A = 0.8;
x1 = A.^n;
x2 = A.^(-n);
if (x1 == x2)
    disp('"The given signal is even!"')
elseif (x1 == (-x2))
    disp('"the given signal is odd!"')
else
    disp('"The given signal in nither odd nor even!"')
end

xe = ( x1 + x2 )/2;
xo = ( x1 - x2 )/2;
tiledlayout(2,2); nexttile;
stem(n,x1);
xlabel("n");
ylabel("x1(n)");
title("signal x(n)")
nexttile;
stem(n,x2);
xlabel("n");
ylabel("x2(n)");
title("signal x(-n)")
nexttile;
stem(n,xe);
xlabel("n");
ylabel("xe(n)");
title("Even Part of x(n)")
nexttile;
stem(n,xo);
xlabel("n");
ylabel("xo(n)");
title("Odd Part of x(n)")
0 Commenti
Risposta accettata
  Chunru
      
      
 il 20 Set 2022
        close; clear; clc;
n = -5:5;
A = 0.8;
x1 = A.^n;
x2 = A.^(-n);
if all(x1 == x2)        % use all here to ensure all elements of x1 and x2 are same
    disp('"The given signal is even!"')
elseif all(x1 == (-x2))          
    disp('"the given signal is odd!"')
else
    disp('"The given signal in nither odd nor even!"')
end
xe = ( x1 + x2 )/2;
xo = ( x1 - x2 )/2;
tiledlayout(2,2); nexttile;
stem(n,x1);
xlabel("n");
ylabel("x1(n)");
title("signal x(n)")
nexttile;
stem(n,x2);
xlabel("n");
ylabel("x2(n)");
title("signal x(-n)")
nexttile;
stem(n,xe);
xlabel("n");
ylabel("xe(n)");
title("Even Part of x(n)")
nexttile;
stem(n,xo);
xlabel("n");
ylabel("xo(n)");
title("Odd Part of x(n)")
Più risposte (1)
  KSSV
      
      
 il 20 Set 2022
        You case x1, x2 are vectors and you cannot use the condition like that. 
You have to use like shown below. 
n = -5:5;
A = 0.8;
x1 = A.^n;
x2 = A.^(-n);
idx = x1 == x2 ;
[x1(idx)' x2(idx)'] 
[x1(~idx)' x2(~idx)'] 
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




