Modify matrix of long length to match another using unique datum values

1 回表示 (過去 30 日間)
Eric Escoto
Eric Escoto 2019 年 7 月 26 日
コメント済み: Eric Escoto 2019 年 7 月 26 日
Hello,
I have two matrices that are different lengths. Each of these matrices has a matching datenum value and record number in the first and second column, respectively.
I am trying to reduce rows of one matrix to the other by removing all rows that don't have matching datnum values.
Some basic and short example data similar to what I'm working with:
matrix1 = [datenum_A recordnumber_C value value value value; datenum_B recordnumber_C value value value value; datenum_C recordnumber_C value value value value];
matrix2 = [datenum_A recordnumber_C value value value value; datenum_B recordnumber_C value value value value; datenum_C recordnumber_C value value value value; datenum_D recordnumber_D value value value value; datenum_E recordnumber_E value value value value; datenum_F recordnumber_F value value value value];
In this case, I would like to remove the last three rows of data from matrix2 becasue the matching datenum and recordnumbers are not in matrix1.
The goal is to have two matrices that are exactly the same row count.
Any help would be greatly appreciated.

採用された回答

the cyclist
the cyclist 2019 年 7 月 26 日
編集済み: the cyclist 2019 年 7 月 26 日
Sounds like you need the ismember command, possibly with the 'rows' argument.
The exact thing you want to do (matches only in first column, or in both?) was not quite clear to me. But you'll need something like
tf = ismember(matrix2(:,1),matrix1(:,1));
to identify the elements of the first column of matrix2 that are also in matrix1.
  2 件のコメント
Eric Escoto
Eric Escoto 2019 年 7 月 26 日
Hi Cyclist,
To clarify, I can use either datenum or a combination of datenum and record number. However, it is much simpler to just use datenum.
Your method produced a logical and let me know what rows are or are not similar. How can I now remove these rows from the longer matrix in question?
Eric Escoto
Eric Escoto 2019 年 7 月 26 日
Ah, I think I got it.
I used:
tf_1 = matrix2(tf, :);
This seemed to have produced the correct size matrix matching matrix1.
Thanks for the help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by