Incorrect dimensions for raising a matrix to a power

3 views (last 30 days)
Lea Abi nassif
Lea Abi nassif on 20 May 2022
Edited: Torsten on 20 May 2022
Hello, i have this code that used to work on an older version of matlab but it's not on the new version and i can't seem to find the solution
PS : I'm using this code in another one
function G=gaussh(A,L,np)
% A amplitude de la gaussienne
% L largeur a mi-hauteur
% np nombre de points
x0=np/2+1;%2049;%257%256; %centre de la gaussienne =Lh/2
s=L/(2*sqrt(2*log(2))); % ecart type de la gaussienne
for x=1:1:np%512 %%
G(x)=A*exp(-(x-x0)^2/(2*(s^2)));
end

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 20 May 2022
Just set a debug-stop in that function and check what dimensions your variables have:
dbstop in gaussh
Or just at the point where you get the error:
dbstop if error
re-run the function (step-by-step in the first case with dbstep) and check the variables with the normal means (whos, size etc).
HTH

More Answers (1)

Voss
Voss on 20 May 2022
Check that your inputs to gaussh are all scalars, because it looks like all three input arguments to the function gaussh are expected to be scalars, and it works if you do that:
gaussh(1,4,10)
ans = 1×10
0.0131 0.0625 0.2102 0.5000 0.8409 1.0000 0.8409 0.5000 0.2102 0.0625
But if either (or both) of the second and third inputs are not scalars, you get the error you saw:
try
gaussh(1,4,[10 10])
catch e
disp(e.message);
end
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
try
gaussh(1,[4 4],10)
catch e
disp(e.message);
end
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
try
gaussh(1,[4 4],[10 10])
catch e
disp(e.message);
end
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
(And if the first argument is not a scalar, you get a different error:)
try
gaussh([1 1],4,10)
catch e
disp(e.message);
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
function G=gaussh(A,L,np)
% A amplitude de la gaussienne
% L largeur a mi-hauteur
% np nombre de points
x0=np/2+1;%2049;%257%256; %centre de la gaussienne =Lh/2
s=L/(2*sqrt(2*log(2))); % ecart type de la gaussienne
for x=1:1:np%512 %%
G(x)=A*exp(-(x-x0)^2/(2*(s^2)));
end
end

Tags

Community Treasure Hunt

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

Start Hunting!