how to exchange matrix rows depending on distance
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
hi guys, i have the following matrix A with distance D1 and matrix B with distance D2.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146119/image.png)
i want a code to exchange A rows with B rows which have higher distance, as moving row 2 in B to matrix A row 2 and remove the old row 2 in A. the output should be:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146120/image.png)
how to do this guys?
0 Commenti
Risposta accettata
Guillaume
il 16 Nov 2014
Modificato: Guillaume
il 16 Nov 2014
Hum, I'm sure I've seen a very similar question (same illustration) in the past. Is this homework?
find the index of the rows you want to exchange:
idx = find(D2 > D1);
And use that to copy the rows of B in A:
A(idx, :) = B(idx, :);
Or, using logical indexing:
A(D2>D1, :) = B(D2>D1);
3 Commenti
Guillaume
il 16 Nov 2014
I can't test your code, I don't have the stats toolbox (no pdist) and you haven't given A and B.
In any case,
A = [1 2 3 4]'
B = [11 12 13 14]'
D1 = [0 14 15 16]' %as in your example
D2 = [0 16 19 14]' %as in your example
A(D2>D1) = B(D2>D1)
returns
A =
1
12
13
4
which is what you asked.
Whatever problem you're having, it's not to do with my bit of code.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!