how to swap rows between matrix?

hi guys,,, i have two matrix A & B, and i want to swap the rows between these matrix according to their Distance. the swap should be from B to A.it swap A rows with lower distance with B rows which have higher Distance. like row no.2 has D=14 and row 2 in B has D=16, so B_row should be swapped with A_row. how to do this? the matrix is below. help needed guys.

2 Comments

Adam
Adam on 24 Oct 2014
Edited: Adam on 24 Oct 2014
I'm not quite sure I fully understand the extent of the problem here. Can you give a full example of expected output?
e.g. I understand you want to swap row 2 of A with row 2 of B based on the distance in each, but that brings a few questions (and probably others):
  • What is your algorithm for going down all rows of the matrix? Can row n of A only be swapped with row n of B based on comparing their distance or do you want to end with all the distances in B being shorter than all those in A or something else?
  • As I understand it D is the distance from the 1st row of the matrix as a simple number of bits that are different. However, if you swap a row from B into A then wouldn't its distance then be measured against the top row of A rather than the top row of B which gives it its current distance?
I don't think I have time to consider a complete answer myself, but hopefully clarification on my questions will help someone else provide you with more of an answer.
this algorithm should run for all the rows comparing their distance, if the distance of row n of A is shorter than row n of B, then it should swap them, and swap all the rows according to this condition.

Sign in to comment.

 Accepted Answer

Presuming you mean literally the simple-minded swap row-by-row depending on the D between the two, it's pretty simple.
Given A and B,
ixless=A(:,2)<B(:,2); % the offending rows
C=A; % need temporary copy
A(ixless,:)=B(ixless,:); % move needed elements of B into A
B(ixless,:)=C(ixless,:); % ditto to put A into B (C is still original A)
clear C

12 Comments

it didn't compare distance.the output is the same.
i have saved the distance of A on matrix M.
and distance of B on matrix Z.
now i have four matrix,
and the output of A came without change,
how to solve it?
ixless = M < Z;
in-place of dpb's 1st line should work in that case with the rest of his answer.
"i have saved the distance of A on matrix M and distance of B on matrix Z. now i have four matrix,..."
I'd ask WHY create four variables when two (or even 1) would do just as well?
But, if there is a real reason to do so then obviously, yes, you need to compare the distances wherever they are, not where they're not.
sara, make it easy for us to help you by giving us code that instantiates A and B, and D, M, Z or whatever other inputs are needed. We're especially interested in that case that you said did not work with dpb's code.
And does it work now or not?
It doesn't work yet....
Adam
Adam on 24 Oct 2014
Edited: Adam on 24 Oct 2014
That is likely because you are mixing up variables. Now you have no matrix A, instead you have M and B with distances in D and X rather than what you said above.
So now you need:
ixless = D < X;
This is the type of thing you should be able to pick up though just from understanding dpb's solution and applying it as a concept rather than just a straight copy and paste if you have changed around your variable names.
sara
sara on 25 Oct 2014
Edited: dpb on 25 Oct 2014
I try the code but it doesn't give the right answer. it bring different rows.i think the problem is with this line:
ixless=D(:)<Z(:);
C=M;
M(ixless,:)=B(ixless,:);
B(ixless,:)=C(ixless,:);
maybe it didn't check row by row.
OK, I wasn't familiar with squareform and so I looked it up...it looks like your choice is reasonable assuming the distances you're looking at are the subsequent of each row compared to the preceding. For the sample arrays you gave of M and B I get
>> [D X D<X]
ans =
0 0 0
16 14 0
19 15 0
14 16 1
14 16 1
15 14 0
which shows only the 4th and 5th rows will end up being swapped. Is that not what you get?
If not, show us complete input/output and what you would expect instead of what you get.
dpb
dpb on 25 Oct 2014
Edited: dpb on 25 Oct 2014
And, indeed, the logic above would do that if D were associated with A and X with B by comparison to the third column above. Since I've already got the results computed from your previous example, let's create a dummy set of arrays A and B that are easy to compare visually as the swap arrays--
>> A=(1:6)'; B=(11:16)'; % easy to tell which is which after swapp
>> ixless=D<X % Use same _D_ and _X_ distance vectors from above
ixless =
0
0
0
1
1
0
>> C=A; % save the temporary copy of A
A(ixless,:)=B(ixless,:);
B(ixless,:)=C(ixless,:);
>> [A B]
ans =
1 11
2 12
3 13
14 4
15 5
6 16
>>
Voila!!! As we had determined previously, for that particular set of arrays your distances showed only the 4th-5th needed swapped which is what is clearly visible in the result above where it's easy to discern from which array the original value came.
NB: this is back to the original definition of the distance vector you had; I erred in not looking at the logic there in sufficient depth initially given that I didn't think thru the pdist output carefully enough first time...sorry, mea culpa there.
But the logic of the swapping does work as advertised if you get the right distance vector associated with the right array and use the proper sequence of storing to the temporary location the one that's overwritten first.
ADDENDUM
I went back and deleted my previous comments wherein I got off in the weeds on the distance vector; that's a red herring. I'm not sure now where you're going wrong.
If the above doesn't resolve the issue, again shows us the exact complete case that doesn't produce the answer you expect so we can see what you did in it's entirety that caused it.
this is good,but how to get the output as matrix (with the swapped rows)?
What you mean? The above uses whatever matrices A and B are; substitute variable names appropriately for your case.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 24 Oct 2014

Commented:

dpb
on 25 Oct 2014

Community Treasure Hunt

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

Start Hunting!