필터 지우기
필터 지우기

Unexpected, unexplained, difference between Matrix linear index and row and column subscripts

조회 수: 1 (최근 30일)
Good Morning;
writing a script I found myself discovering a difference i cannot explained.
for i=size(P,1)*size(P,2)
if A(i)>1
A(i)=1;
B(i)=P(i).*(2./(gamma+1));
C(i)=P(i)-(P(i)-B(i))./eta(type);
D(i)=Q(i).*(C(i)./P(i)).^(gamma./(gamma-1));
E(i)=sqrt(gamma.*R.*B(i));
F(i)=E(i);
end
end
writing the above code the end results were not as expected. i then wrote the same operations nesting 2 for cycles (row and column subscripts)
for i=1:size(P,1)
for j=1:size(P,2)
if A(i,j)>1
A(i,j)=1;
B(i,j)=P(i,j).*(2./(gamma+1));
C(i,j)=P(i,j)-(P(i,j)-B(i,j))./eta(type);
D(i,j)=Q(i,j).*(C(i,j)./P(i,j)).^(gamma./(gamma-1));
E(i,j)=sqrt(gamma.*R.*B(i,j));
F(i,j)=E(i,j);
end
end
end
this, strangely to me, gave me a different end result. I could not understand why.
A,B,C,D,E,F,P,Q
are all matrices of same dimension and enter both cycles with same values, same goes for
gamma,eta, type
I would appreciate if someone would be able to explain the difference in the two methods, that should have behaved identically in my mind.
tips on more efficient/elegant ways of achieving the above are also welcome

채택된 답변

Bruno Luong
Bruno Luong 2021년 10월 26일
편집: Bruno Luong 2021년 10월 26일
Try to change
for i=size(P,1)*size(P,2)
to
for i=1:size(P,1)*size(P,2)
You light also learn to use debugger so such task.
  댓글 수: 1
Jad Michele Samuel Musallam
Jad Michele Samuel Musallam 2021년 10월 26일
편집: Jad Michele Samuel Musallam 2021년 10월 26일
thanks to both.
well, that's embarassing. I never considered learning to use the debugger. Might consider now, thak you a lot

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

추가 답변 (1개)

Esen Ozbay
Esen Ozbay 2021년 10월 26일
편집: Esen Ozbay 2021년 10월 26일
You have forgotten to write 1: in the first line.
for i=size(P,1)*size(P,2)
must be
for i = 1:size(P,1)*size(P,2)
You probably got 0's in all elements except one in the arrays you obtained.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by