필터 지우기
필터 지우기

Why do I get an error "Matrix dimensions must agree."

조회 수: 1 (최근 30일)
Rertan Ertan
Rertan Ertan 2024년 2월 4일
댓글: Rena Berman 2024년 3월 15일
I write my code with "for loops" I get working code but when i try to vectorized it get an error "Matrix dimensions must agree". What do you suggest to solve it ?
dy = 0.010;
dx = 0.010;
Nx = 100;
Ny = 100;
STEPS = 500;
Hx = zeros(Nx,Ny);
Hy = zeros(Nx,Ny);
Ez = zeros(Nx,Ny);
for a = 1:STEPS
Hx(1:Nx,1:Ny) = Hx(1:Nx,1:Ny);
Hy(1:Nx,1:Ny) = Hy(1:Nx,1:Ny);
Ez(1:Nx,1:Ny) =(Hy(1:Nx,1:Ny)-Hy(1:Nx-1:Ny))/dx-(Hx(1:Nx,1:Ny)-Hx(1:Nx,1:Ny-1))/dy;
end

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2024년 2월 4일
이동: Matt J 2024년 2월 4일
Why are you using a for loop for a task that is not changing with iterations?
"Why do I get an error "Matrix dimensions must agree.""
Because you are trying to add a (Nx)x(Ny) matrix with (Nx-1)x(Ny) matrix, while defining Ez.
  댓글 수: 2
Rertan Ertan
Rertan Ertan 2024년 2월 4일
이동: Matt J 2024년 2월 4일
This is just an small segment of the code, in reality values inside the loop changing for ever iterations. I stucked with Ez array
Dyuman Joshi
Dyuman Joshi 2024년 2월 5일
"This is just an small segment of the code, in reality values inside the loop changing for ever iterations."
Then share all the relevant code and specify what you are trying to do and what is the expected output.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by