Circular indexing of a 2D array

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.

 Accepted Answer

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

More Answers (0)

Products

Release

R2015a

Community Treasure Hunt

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

Start Hunting!