필터 지우기
필터 지우기

how to exchange matrix rows depending on distance

조회 수: 3 (최근 30일)
janny
janny 2014년 11월 16일
편집: janny 2014년 11월 16일
hi guys, i have the following matrix A with distance D1 and matrix B with distance D2.
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:
how to do this guys?

채택된 답변

Guillaume
Guillaume 2014년 11월 16일
편집: Guillaume 2014년 11월 16일
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
Guillaume
Guillaume 2014년 11월 16일
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.
janny
janny 2014년 11월 16일
편집: janny 2014년 11월 16일
thank you very much... its working fine now,,,,

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

MA
MA 2014년 11월 16일
clear all
close all
clc;
A=[11 22 33 44;0 14 15 16]'
B=[66 77 88 99;0 16 19 14]'
for i=1:4
if A(i,2)<B(i,2)
A(i,1)=B(i,1);
end
end
A
  댓글 수: 1
Guillaume
Guillaume 2014년 11월 16일
M.A., Your loop is equivalent to:
idx = A(:,2)<B(:,2);
A(idx, :) = B(idx, :);
No need for a loop.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by