Using sign function in Matlab

3 views (last 30 days)
Sergio Manzetti
Sergio Manzetti on 28 May 2020
Answered: Tommy on 28 May 2020
Hi, I try to run the following in MATLAB:
[D,x] = cheb(N);
D2 = D^2;
D = D(2:N,2:N); D2 = D2(2:N,2:N); x = x(2:N);
A = -1*(sign(x))*D2 + (diag(x))^2;
but I get the following error:
Error using *
Inner matrix dimensions must agree.
Error in airy_demo (line 32)
A = -1*(sign(x))*D2 + (diag(x))^2;
Error in switch_demo (line 52)
mtx = feval(routine,N);
Error in eigtool_switch_fn (line 383)
ps_data = switch_demo(fig,ps_data,the_demo);
Error while evaluating Menu Callback.
This happens only when I use the sign function. Is it given incorrectly? Thanks

Answers (1)

Tommy on 28 May 2020
sign(x) returns a matrix the same size as x. If the dimensions of x (and therefore sign(x)) are N-1 x 1 and the dimensions of D2 are N-1 x N-1, then these dimensions are incompatible for matrix multiplication of sign(x)*D2 (unless N = 2).
D2*sign(x), on the other hand, is valid matrix multiplication and will result in a vector with size N-1 x 1. It looks like you are then adding (diag(x)^2), which has size N-1 x N-1, so I'm not sure D2*sign(x) is what you are looking for.
Can you provide more info on what you are trying to do?


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!