필터 지우기
필터 지우기

Another data manipulation question,need some help!

조회 수: 1 (최근 30일)
Xiao Tang
Xiao Tang 2012년 6월 23일
There are two matrix A and B.
A =
15 4534 0 31
10 0 346 0
10 342 124 11
20 345 0 13
25 73 311 15
15 0 0 14
10 0 153 16
15 532 129 19
20 153 0 0
B =
10 4324 13976 7645
15 7675 3821 14510
20 13198 0 3874
25 4321 4321 4321
According to the first column, replace the rest columns of A with B.
The result should be:
C =
15 7675 3821 14510
10 4324 13976 7645
10 4324 13976 7645
20 13198 0 3874
25 4321 4321 4321
15 7675 3821 14510
10 4324 13976 7645
15 7675 3821 14510
20 13198 0 3874
I used 2 loops(shown as follows) to get the result. But I was wondering if there is an easier way.
C(:,1) = A(:,1);
for i = 1: length(A)
for n = 1:length(B)
if A(i,1) ==B(n,1)
C(i,2:4) = B(n,2:4);
end
end
end
Any suggestion is welcomed!

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 23일
[tf, idx] = ismember( A(:,1), B(:,1) );
A(tf,:)= B(idx(tf), :);
  댓글 수: 1
Xiao Tang
Xiao Tang 2012년 6월 23일
Thanks @Walter Roberson. The ismember function and logical index should be more efficient than my loop!

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 6월 23일
[b b b] = unique(A(:,1));
C = B(b,:)
  댓글 수: 1
Xiao Tang
Xiao Tang 2012년 6월 24일
What can I say, man. You gave me great answers to all my questions.Thanks a million!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by