Recall elements inside the rows of a matrix based on a predefined array

60 views (last 30 days)
luca
luca on 11 Sep 2019
Answered: Fabio Freschi on 11 Sep 2019
Given the following matrix
M= [ 58 116.4 78.5 72.5 163.6 60.4 100 66 117 65 55;
58.3 123.1 89.8 87.4 167.7 61.3 100 66 117 65 55;
58.2 123.1 78.3 80.4 152.3 61.3 100 66 117 65 55;
58 125.4 84.5 80.1 169.2 61.6 100 66 117 65 55;
58 105.3 86.5 84.5 220.8 63.8 100 66 117 65 55;
58.4 100.8 77 66.1 172.5 54.3 100 66 117 65 55;
58 116.4 94.3 91 199.5 65.6 100 66 117 65 55;
58.1 116.4 113.3 7.1 166.9 60.8 100 66 117 65 55;
58.3 103 72.9 56.4 212.7 60.6 100 66 117 65 55;
58 123.1 130.6 87.9 169.5 61.1 100 66 117 65 55;
58 98.6 76.1 65.1 154.2 60 100 66 117 65 55;
58 118.7 81.2 72.5 170.5 60.8 100 66 117 65 55;
58 103 83.1 65.1 154.2 60 100 66 117 65 55;
58.1 105.3 70.2 66.1 148.1 59.5 100 66 117 65 55;
58 118.7 81.2 72.5 169.5 60.8 100 66 117 65 55]
I want to recall the elements in the rows based on a vector
V= [1 3 4 5 6 7 3 2 1 ]
So in this case the new Matrix A will have as first raw the first row of M, as second raw the 3rd row of M, as third raw the 4th row of M, as fourth row the fifth row of M and so on.
Every number inside V recall the specified row of M, creating the new matrix A
A = [58 116.4 78.5 72.5 163.6 60.4 100 66 117 65 55;
58.2 123.1 78.3 80.4 152.3 61.3 100 66 117 65 55;
58 125.4 84.5 80.1 169.2 61.6 100 66 117 65 55;
58 105.3 86.5 84.5 220.8 63.8 100 66 117 65 55;
and so on
The number of raw of A will be equal to the length of the vector V, that could be longer than the one writted above

Accepted Answer

Fabio Freschi
Fabio Freschi on 11 Sep 2019
A = M(V,:);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by