How to associate the max number of a row from one matrix to a number from a different matrix

1 view (last 30 days)
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
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?
Nicholas Saint
Nicholas Saint on 24 Oct 2019
Edited: Nicholas Saint on 24 Oct 2019
Thanks for the quick reply and sorry if it's so confusing. Yes, something like that. Column 1, 2, and 3 of cycle array is associated with value 1, 2, and 3 of the same row in the X array. When comparing the max() values of each row of cycle array, it will determine in the cycle array which column gets which value of that row by reverse order. So:
if max(cycle2(1,:)) > max(cycle2(3,:)) && max(cycle2(1,:)) > max(cycle2(2,:))
if min(cycle2(1,:)) == cycle2(1,3)
X(4,[1 3]) = X(4,[3 1]);
elseif min(cycle2(1,:)) == cycle2(1,2)
X(4,[1 2]) = X(4,[2 1]);
end
I'd do that for multiple conditions for each row until the max() row, median() row, and min() row are established with its inverse order min() to max() value for that row. It will then rearrange the associated value for X accordingly.
Hope that makes more sense.

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!