My Tp matrix is as follows,
1 2 3 7
Tp= 4 5 6 5
7 8 9 8
The code is,
for i=1:3
for j=1:4
if i==1 && j==1
T(i,j)=Tp(i,j-1)
end
if i==2 && j==4
T(i,j)=Tp(i,j+1)
end
end
end
I want the answer of the first IF condition to be T(i,j)=7, which is the element on the other side of the Tp matrix.
Second IF condition answer to be T(i,j)=4, which is the element on the other side of the Tp matrix.
How will I be able to do this?
Thanks in advance.

 채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2019년 5월 30일

0 개 추천

Something like this solves this problem:
for i=1:3
for j=1:4
if i==1 && j==1
T(i,j)=Tp(i,1 + mod(4*123+j-2,4))
end
if i==2 && j==4
T(i,j)=Tp(i,1 + mod(4*123+j+1,4))
end
end
end
You might have to fiddle around with mod/rem if you want to modify this to more general cases...
You could also have a look at the file exchange for something like circular indexing...
HTH

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by