## Matrix dimensions and unbalanced bracket problem

jeff m

### jeff m (view profile)

님이 질문을 제출함. 30 Aug 2018
최근 활동 jeff m

### jeff m (view profile)

님이 댓글을 추가함. 31 Aug 2018
Rik

### Rik (view profile)

님이 답변을 채택함.
So I have an electrical signal that is given in this form
I'm trying to execute this script in MATLAB to show it visually
function u=sign_sd(t)
% signal
u = [-0.5*t*10e6 * t((t>=0)&(t<=2e-6)),...
-cos[(pi*10e6*(t-2*10e-6))] * t((t>2)&(t<=4e-6)),...
1 * t((t>4)&(t<=6e-6)),...
0 * t((t>6)&(t<=8e-6)),...
0 * t(t>8e-6)];
.
% signal graph
t=0:1e6:10e6;
u=sign_sd(t);
plot(t,u),grid on
But any way I try to change it, it gives an error.
Error: File: sign_sd.m Line: 5 Column: 9
Unbalanced or unexpected parenthesis or bracket.
Error in sign_g (line 3)
u=sign_sd(t);
.
size(t) = 1 11
size(u) = 1 21
Currently besides unbalanced matrix dimensions problem there are unbalanced brackets. Possibly where -cos starts. Can any give an advice on how to fix it, so it works? I've also tried to use zeros(size(t)) in '% signal graph' code, but that didn't solve it.

#### 댓글 수: 1

30 Aug 2018
Try to remove the square brackets only for cos.

로그인 to comment.

R2017b

## 답변 수: 2

Rik

### Rik (view profile)

님의 답변 30 Aug 2018
채택된 답변

Here's a complete plot. You also forgot to put in extra exponents for the lower bounds.
%t=0:1e-6:10e-6;
t=linspace(0,10e-6,200);
figure(1),clf(1)
plot(t,sign_sd(t))
axis tight
ax = gca;
ax.XAxis.Exponent = -6;
function u=sign_sd(t_master)
u=NaN(size(t_master));
L=(t_master>=0)&(t_master<=2e-6);
t=t_master(L);
u(L)=-0.5*t*10^6;
L=(t_master>2e-6)&(t_master<=4e-6);
t=t_master(L);
u(L)=-cos(pi*10e6*(t-2*10e-6));
L=(t_master>4e-6)&(t_master<=6e-6);
u(L)=1;
L=(t_master>6e-6)&(t_master<=8e-6);
u(L)=0;
L=t_master>8e-6;
u(L)=0;
end

jeff m

### jeff m (view profile)

31 Aug 2018
Thank you, this works.

로그인 to comment.

Titus Edelhofer

### Titus Edelhofer (view profile)

님의 답변 30 Aug 2018

Hi Jeff,
I would rather use indexing instead of multiplication, something like
u = zeros(size(t));
% first 2 microseconds:
idx = t>=0 & t<=2e-6;
u(idx) = -0.5 * t(idx) * 1e6;
etc.
Titus

jeff m

31 Aug 2018
Thanks.

로그인 to comment.

Translated by