Is there a way to reference a matrix so that it knows its position in another matrix?

조회 수: 1 (최근 30일)
Is there a better way to reference this table? the i and j columns are referring to a specific point inside an 11x14 matrix. Looking at the example code I have posted, Q is an 11x14 matrix. My code is stating that Q(4,10)=... by making this convoluted reference Q((Well(nn,6),Well(nn,7)). Is there a better way to do this?

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 20일
No, that is appropriate code.
As a matter of clarity I would use a temporary vector, something like
for nn=1:NumWell
thiswell = Well(nn,:);
if thiswell(2) == 5
i = thiswell(6); j = thiswell(7);
Q(i, j) = Q(i, j) - thiswell(4);
Rate1(i, j) = - thiswell(4);
end
end
  댓글 수: 2
zephyr21
zephyr21 2016년 6월 20일
That helps tidy up the code a bit. But the reason I was asking is because in another string of code following this, I need to say that if the pressure of a specific spot in another 11x14 matrix falls below the number in the table, then make the rate zero
Walter Roberson
Walter Roberson 2016년 6월 20일
The same principles apply: for clarity, assign to temporary variables. This is not necessarily going to lead to faster code, but unless the code is going to be executed a lot, the larger cost is in programming and debugging, so write first to be able to understand the code.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by