keep the same row data from two matrix

2 ビュー (過去 30 日間)
zhengyang shang
zhengyang shang 2021 年 3 月 25 日
コメント済み: Jan 2021 年 3 月 25 日
i have a total data matrix a(253*2), now i just want to keep the data from b(223*1) and get the whole data is c(223*2), which means that keep the same data between a&b. how should i do?
  3 件のコメント
zhengyang shang
zhengyang shang 2021 年 3 月 25 日
of course not, first need to find the data in the first row in the b and also in the first row in a, then output the c
Mathieu NOE
Mathieu NOE 2021 年 3 月 25 日
OK so you have to use find the get the similar values from col 1 in a and b
ind = find(a(:,1) == b);
c = a(ind,:);

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

回答 (1 件)

Jan
Jan 2021 年 3 月 25 日
This is a job for intersect or ismember.
[~, ia, ib] = intersect(a(:, 1), b);
c = [b(ib), a(ia, 2)]
  2 件のコメント
zhengyang shang
zhengyang shang 2021 年 3 月 25 日
actually it still doesn't work because it tell me a and b are not contain same variable. i upload the data the matrix a is on the sheet1 and matrix b is on the sheet2.
Thanks so much!
Jan
Jan 2021 年 3 月 25 日
I cannot follow you. The tables do share at least some common data. So my code is expected to work. If it does not work for you, please post your code.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by