필터 지우기
필터 지우기

error >> Matrix dimensions must agree. How to fix

조회 수: 15 (최근 30일)
Ofer Aluf
Ofer Aluf 2024년 4월 23일
편집: Torsten 2024년 4월 23일
Hello,
I have script:
d=0.025:0.0025:1; % d=0.125*Lambda to 12.5*Lambda
Lambda=1;
% Ga function
Lambda=0.2;l1= Lambda/4;zi=l1;si=1;k=(2*pi)/Lambda;
z=0:Lambda/400:Lambda/4;
y= sqrt(d.^2+(z-zi));
Arrays have incompatible sizes for this operation.
Gfun=@(z)(1./y).*exp(-1i.*k.*y).*exp(-1i.*k.*si.*z);
% R=sqrt(d^2+(z-zi));
% evaluate the integral from z=0 to l1
Ga=integral(Gfun,0,Lambda/4);
Question? Why i got message "Matrix dimensions must agree"? and how to fix it??

답변 (3개)

Anjaneyulu Bairi
Anjaneyulu Bairi 2024년 4월 23일
편집: Anjaneyulu Bairi 2024년 4월 23일
Hi,
Below line causing the error and it says "z" and "zi" are incompatible sizes for this operation.
y= sqrt(d.^2+(z-zi));
The "d.^2" variable has size of 1*391 double and "(z-zi)" has size of 1*101 double. Make sure you have declared these variables correctly to avoid these kind of errors.
You can refer below documentation links to know more information on arrays
I hope above information is helpful to resolve your query

Lokesh
Lokesh 2024년 4월 23일
Hi Ofer,
It appears that you are encountering the error "Matrix dimensions must agree" while running your MATLAB script.
This issue arises because the dimensions of "d.^2" (1x391) and "z-zi" (1x101) in the equation "y= sqrt(d.^2+(z-zi));" are not compatible, leading to the error.
Please refer to the following MATLAB documentation for more information about compatible array sizes for basic operations:
Hope this helps!

Torsten
Torsten 2024년 4월 23일
편집: Torsten 2024년 4월 23일
z doesn't need to be prescribed in array form because you integrate with respect to z. Here I assume that the integration variable and the z in the expression for y are the same.
If you mean to evaluate your integral for the d-values that you defined, you can use
d=0.025:0.0025:1; % d=0.125*Lambda to 12.5*Lambda
% Ga function
Lambda=0.2;l1=Lambda/4;zi=l1;si=1;k=(2*pi)/Lambda;
y= @(d,z)sqrt(d^2+(z-zi));
Gfun =@(d,z)(1./y(d,z)).*exp(-1i.*k.*y(d,z)).*exp(-1i.*k.*si.*z);
Ga=arrayfun(@(d)integral(@(z)Gfun(d,z),0,Lambda/4),d);
plot(d,[real(Ga);imag(Ga)])

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by