UUser196
UUser196

Reputation: 25

MATLAB summing arrays with different index

I have to x[n − 2] + x[n + 2] make this sum but I couldn't find how to do it. I searched for hours but there no enough sources. By the way x[n]=u[n]-u[n-1] for 0 ≤ n ≤ 5:. I write the the code of x[n] but couldn't add these two.

t = 0:5;
unitstep1 = zeros(size(t)); 
unitstep2 = zeros(size(t));
unitstep1(t>=0) = 1;
unitstep2(t>=1) = 1;
x = unitstep1 - unitstep2

Upvotes: 1

Views: 123

Answers (2)

Max
Max

Reputation: 4045

How about just shifting the array elements?

idx = 1:length(x);
offset = 2;

x_diff = x( idx(1:end-offset) ) - x( idx(offset+1:end) );

Of course, the array x_diff is shorter than the original x. You will always need to define the first entries... Anyway, this is a single-line command which avoids looping.

Upvotes: 2

MichaelTr7
MichaelTr7

Reputation: 4767

Two methods include using anonymous functions or simulating a shift and padding the array with zeros to ensure the arrays have the same size. I typically opt-in to using anonymous functions in this case unless you specifically need the aggregate unit step data for further steps. If you'd like to plot discrete points in terms of n I suggest using the stem() function in place of the typical plot() function. If you'd like you to like to plot as a continuous staircase/digital signal you can use the stairs() function. If you need the resultant vector method 2 may be an option.

Plotting Anonymous Functions

Method 1: Using Anonymous Functions

Anonymous functions can be identified by the @() term. Within the bracket of the @() term are the input variables/dependent variables. In this case, that term is n.

n = 0:5;
%Defining the unit step function%
u = @(n) 1.0.*(n >= 0);

%Defining function x in terms of the unit step function%
x = @(n) u(n) - u(n-1);

%Defining f in terms of the funtion x[n] or x(n)%
f = @(n) x(n-2) + x(n + 2);

%Axis limits%
X_Minimum = -0.5;
X_Maximum = 5.5;
Y_Minimum = -0.5;
Y_Maximum = 1.5;

%Plotting x[n] as staircase/digital signal%
subplot(2,2,1); stairs(n,x(n));
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Staircase Format x[n] = u[n] - u[n-1]");
grid;

%Plotting x[n] as discrete signal%
subplot(2,2,2); stem(n,x(n));
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Discrete Points x[n] = u[n] - u[n-1]");
grid;

%Plotting f[n] as staircase/digital signal%
subplot(2,2,3); stairs(n,f(n));
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Staircase Format f = x[n − 2] + x[n + 2]");
grid;

%Plotting f[n] as discrete signal%
subplot(2,2,4); stem(n,f(n));
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Staircase Format f = x[n − 2] + x[n + 2]");
grid;

Method 2: Simulating Shift by Truncating and Padding with Zeros

This method uses array x and tries to simulate the idea of shifting it to create x[n-2] and x[n+2]. The signal x[n-2] is essentially the signal moved the right by two so if we pad the array with two zeros at the beginning and truncate the end of the signal by two we can achieve this effect. Similarly, this can be done for x[n+2] except the zeros are added to the end and the beginning of the signal is truncated. The end term is a method of indexing the last element in the vector/matrix.

n = 0:5;
unitstep1 = zeros(size(n)); 
unitstep2 = zeros(size(n));
unitstep1(n>=0) = 1;
unitstep2(n>=1) = 1;

x = unitstep1 - unitstep2;

%Simulating shitfts%
f = [0 0 x(1:end-2)] - [x(end-3:end) 0 0];

%Axis limits%
X_Minimum = -0.5;
X_Maximum = 5.5;
Y_Minimum = -0.5;
Y_Maximum = 1.5;

%Plotting x[n] as staircase/digital signal%
subplot(2,2,1); stairs(n,x);
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Staircase Format x[n] = u[n] - u[n-1]");
grid;

%Plotting x[n] as discrete signal%
subplot(2,2,2); stem(n,x);
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Discrete Points x[n] = u[n] - u[n-1]");
grid;

%Plotting f[n] as staircase/digital signal%
subplot(2,2,3); stairs(n,f);
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Staircase Format f = x[n − 2] + x[n + 2]");
grid;

%Plotting f[n] as discrete signal%
subplot(2,2,4); stem(n,f);
axis([X_Minimum X_Maximum Y_Minimum Y_Maximum]);
title("Plotting as Staircase Format f = x[n − 2] + x[n + 2]");
grid;

Ran using MATLAB R2019b

Upvotes: 0

Related Questions