How to associate the max number of a row from one matrix to a number from a different matrix
1 view (last 30 days)
Show older comments
I'm a bit of a beginner in matlab. I have two matrices, 1 random 4*3 matrix called X, and another called cycle1. I intend to use cycle1 to determine how I rearrange the elements for row 3 of X in a special way. I want to do this for row 4 and ultimately row m of matrix X.
X =
0.9816 0.8819 0.8555
0.0326 0.1564 0.1904
0.4607 0.3763 0.3689 %This row made the matrix for cycle1
0.6692 0.6448 0.5612
cycle1 =
0.9904 0.9889 0.9888 % This row is accociated with X(:, 1)
0.9463 0.9378 0.9371 % This row is accociated with X(:, 2)
0.9369 0.9270 0.9262 % This row is accociated with X(:, 3)
% column 1 of cycle1 is associated with X(3, 1)
% column 2 of cycle1 is associated with X(3, 2)
% column 3 of cycle1 is associated with X(3, 3)
In a sense, I'm ranking the elements of cycle1 by row (comparing the highest value of each row determines the ranks of the rows) and using it to rearrange the 3rd row of X so that the indices corresponds to the rank. In this case, the rows of cycle1 has a decending rank, where row 1 of cycle1 is greater than row 2 and row 3, and row 2 is greater than row 3. The logic is to give the highest result of the lowest rank, the mid result of the mid rank, and lowest result of the highest rank of the rows of cycle1 to its corresponding values of row 3 of X.
This shows how cycle1 should determine how to rearrange row 3 of X:
0.9904 0.9889 0.9888(X(3,1)) % corresponds to 0.3689
0.9463 0.9378(X(3,2)) 0.9371 % corresponds to 0.3763
0.9369(X(3,3)) 0.9270 0.9262 % corresponds to 0.4607
So elements of row 3 of X will swap to:
0.9816 0.8819 0.8555
0.0326 0.1564 0.1904
0.3689 0.3763 0.4607
0.6692 0.6448 0.5612
The positions will not always be the same if I use this for loop after calculating a matrix from row 4. The loop I wrote will only work for row 3:
if min(cycle1(1,:)) == cycle1(1,3)
X(3,[1 3]) = X(3,[3 1]);
elseif min(cycle1(1,:)) == cycle1(1,2)
X(3,[1 2]) = X(3,[2 1]);
end
if max(cycle1(3,:)) == cycle1(3,3)
X(3,[1 3]) = X(3,[3 1]);
elseif max(cycle1(3,:)) == cycle1(3,2)
X(3,[1 2]) = X(3,[2 1]);
end
I made a cycle2 matrix using row 4 of X. I want to implement the same idea for row 4, but the ranks of each row of cycle2 will vary.
cycle2 =
0.9932 0.9923 0.9908(X(4,1)) % corresponds to 0.5612
0.9670(X(4,2)) 0.9629 0.9553 % corresponds to 0.6692
0.9689 0.9650(X(4,3)) 0.9579 % corresponds to 0.6448
So elements of row 4 of X will swap to:
0.9816 0.8819 0.8555
0.0326 0.1564 0.1904
0.3689 0.3763 0.4607
0.5612 0.6692 0.6448
I'm trying to find a more efficient way to implement this so that it can work for any row of X past row 3, but I can only come up with a mess of nested loops and dozens of lines of code. I'm sure there's a better way of implementing what I'm looking for. Any help is appreciated.
Thanks
2 Comments
dpb
on 24 Oct 2019
Whew! That's tough to follow...
I can't wend my way through the weeds to follow the logic of why the order is what it is...is the association the inverse order of the max() values by row of the cycle array?
Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!