Update matrix from matrix

1 次查看(过去 30 天)
Gavanpreet Singh Bhatia
回答: Tasneem 2022-7-12
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.

回答(1 个)

Tasneem
Tasneem 2022-7-12
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);

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by