Home > Blockchain >  Left and right sides have a different number of elements when trying to define conditions of a perio
Left and right sides have a different number of elements when trying to define conditions of a perio

Time:09-07

I'm trying to plot periodic signal that has an exponent and time conditions, but I'm getting an error in my line one_period(-5 <= t1 & t1 < 0) = exp(10*t1 - 10);.

I'm still very new to MATLAB, so I'm unsure of how to fix this error.

T = 1; 
t1 = linspace(0, T, 100   1); 
t1(end) = [];
one_period = zeros(size(t1));
one_period(-5 <= t1 & t1 < 0) = exp(10*t1 - 10); 
one_period(0 <= t1 & t1 < 5) = 10;
signal = repmat(one_period, 1, 5);
signal_length = 10;
t_signal_length = linspace(0, T*signal_length, signal_length*100   1); 
t_signal_length(end) = [];
figure; 
plot (t_signal_length, signal);

CodePudding user response:

You are getting an error on this line:

one_period(-5 <= t1 & t1 < 0) = exp(10*t1 - 10); 

Because t1 is defined as

T = 1; 
t1 = linspace(0, T, 100   1); 
t1(end) = [];

So it is an array between 0 and 1 with 101 elements, then with the last element removed.

And the condition you've used on the erroneous line is not satisfied for any element of t1, which values are you expecting to be between -5 and 0 in an array defined as values between 0 and 1?

Therefore this -5 <= t1 & t1 < 0 is an array where every value is false, i.e you are assigning into zero indices of one_period, but you are trying to assign something (on the right of the =) which has as many values as t1. One of these does not fit into the other!

MATLAB has pretty good debugging tools, which allow you to add breakpoints, run snippets of code whilst in debug, and view variables as the code progresses. You need to have a clear idea of your expected behaviour and then run through from a breakpoint to identify which part is failing.

If you had a different condition (which actually had some true values) then maybe you just need to use this index on both sides of the assignment so that the number of values matches the number of indicies. This would look something like

bValid = 0.5 <= t1 & t1 < 0.8; % some condition for a subset within (0,1)
one_period(bValid) = exp(10*t1(bValid) - 10); % Note indexing t1(bValid) too

CodePudding user response:

T = 1; %-signal period
t1 = linspace(0, T, 100   1); %-time series from 0 to T
t1(end) = []; %-remove last value
one_period = zeros(size(t1)); %-make function array (all zeros)
one_period(0 <= t1 & t1 < 0.5) = exp(10*t1(0 <= t1 & t1 < 0.5) - 10); %-define the signal
one_period(0.5 <= t1 & t1 < 1) = exp(10*t1(0.5 <= t1 & t1 < 1) - 10);
signal_length = 5;
signal = repmat(one_period, 1, signal_length); %-replicate the signal 5 times
t_signal_length = linspace(0, T*signal_length, signal_length*100   1); %-replicate the time series
t_signal_length(end) = [];
figure; %plot
plot (t_signal_length, signal);

Example plot: https://imgur.com/a/XhZnqId

As always, comments are helpful for knowing what your intentions were but here's what I think you were trying to achieve.

  • Related