Clear Filters
Clear Filters

Update matrix from matrix

1 view (last 30 days)
Gavanpreet Singh Bhatia
Gavanpreet Singh Bhatia on 30 Jan 2018
Answered: Tasneem on 12 Jul 2022
M1 =
1 --> Position 1
2 --> Position 2
1 --> Position 3
2 --> Position 4
M2 =
2 1 3 4
Hi All,
I need your help in coding for a simple program.
I have two Matrices (or Arrays) and I want to update M2 according to M1.
This i tell you how, for instance, whatever number is present in M1 third position gets updated in every index of M2 where number 3 is present. And same goes for all positions of M2.
So, my desired result would be like this {M2 = 2 1 1 2 }
Please help.

Answers (1)

Tasneem
Tasneem on 12 Jul 2022
It is my understanding that you have two Arrays M1 & M2. M2 contains indexes and you want to update M2 according to the values present at those indexes in M1.
The MATLAB code for this is given below and this code is giving the desired result.
m1 = [1,2,1,2];
m2 = [2,1,3,4];
m2(1:length(m2)) = m1(m2);

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!