How to track elements in a matrix that switch their position when converting into another matrix?

2 views (last 30 days)
Hi, Could you please help me find a solution to this problem:
Matrix A gives the index of a set of 2 particles (i;ii) distributed in 10 columns (2*10). Matrix B gives the index of these particles after 1 second and Matrix C gives their index after 2 seconds. For instance, if particle i is introduced at position 7 in matrix A, it would get to position 4 after t time; particle i from 4th column in matrix B would end in column 9 in Matrix C. I would like to track the trajectory of these particles as a function of time and their initial position (see Xi and Xii). For instance, the trajectory of particle i that is introduced at 7th is : from column 7 to column 4 and from 4 to 9 after 2 seconds.
A = [1 2 3 4 5 6 7 8 9 10;1 2 3 4 5 6 7 8 9 10]
B = [1 1 1 1 2 3 4 5 6 7;1 1 1 1 1 1 1 1 2 3]
C= [6 7 8 9 10 10 10 10 10 10;4 5 6 7 8 9 10 10 10 10]
Screen Shot 2019-08-30 at 12.53.53 PM.png
Thanks in advance
Maryam

Accepted Answer

Stephen23
Stephen23 on 30 Aug 2019
>> A = [1,2,3,4,5,6,7,8,9,10;1,2,3,4,5,6,7,8,9,10];
>> B = [1,1,1,1,2,3,4,5,6,7;1,1,1,1,1,1,1,1,2,3];
>> C = [6,7,8,9,10,10,10,10,10,10;4,5,6,7,8,9,10,10,10,10];
>> Xi = A([1,1,1],:); % preallocate
>> Xi(2,:) = B(1,Xi(1,:));
>> Xi(3,:) = C(1,Xi(2,:))
Xi =
1 2 3 4 5 6 7 8 9 10
1 1 1 1 2 3 4 5 6 7
6 6 6 6 7 8 9 10 10 10
>> Xii = A([2,2,2],:); % preallocate
>> Xii(2,:) = B(2,Xii(1,:));
>> Xii(3,:) = C(2,Xii(2,:))
Xii =
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 2 3
4 4 4 4 4 4 4 4 5 6

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!