フィルターのクリア

find where both matrices match

1 回表示 (過去 30 日間)
Danielle Leblanc
Danielle Leblanc 2011 年 8 月 4 日
Hi,
I have matrix A and matrix B. The first column in both of them is a dates column where few observations and A and B coincide. I have created a matrix C where the first column in C is the dates of A and I want to put in the matrix the observations of B where the dates match.
I tried x0=find(ismember(B(:,1),A(:,1))==1); but how can I find the rows in C where i need to put B(x0,2:end)?

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 4 日
I am having a second thought. The first solution requires:
  1. no duplicate dates in A alone and in B alone.
  2. the dates in A and B shall be sorted and in the same order (ascending or descending). If not, the results will be incorrect. Un-comment the line to see the effect.
A=[1:10;1:10]';
B=[2:2:20;20:20:200]';
%B=B(end:-1:1,:);
C=nan(size(A));
C(:,1)=A(:,1);
IndexA=ismember(A(:,1),B(:,1));
IndexB=ismember(B(:,1),A(:,1));
C(IndexA,2)=B(IndexB,2);
A better solution is below. It still requires no duplicate dates which should be the case based on the task.
A=[1:10;1:10]';
B=[2:2:20;20:20:200]';
B=B(end:-1:1,:);
C=nan(size(A));
C(:,1)=A(:,1);
[Dummy,IndexA,IndexB]=intersect(A(:,1),B(:,1));
C(IndexA,2)=B(IndexB,2);
  4 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 8 月 5 日
Or could make it work this way.
[IndexA, locB] = ismember(A(:,1),B(:,1));
C(IndexA,2) = B(locB(IndexA),2);
Oleg Komarov
Oleg Komarov 2011 年 8 月 5 日
The zeros! I always do ismember(A, subset of A), that's why I forgot about zeros, but of course this implies that one is a subset.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by