copying matrix values based on a specific value in another column

2 ビュー (過去 30 日間)
okoth ochola
okoth ochola 2023 年 1 月 20 日
コメント済み: Jiri Hajek 2023 年 1 月 20 日
Hello, suppose we have A=[1 1 1 1 2 2 2 2 3 3 3 3]' and B=[3 4 6 7 8 9 0 12 4 5 6 8]', so that C=[A B]. Is there a way I can copy all element values in column 2 corresponding to say all 1 in column 1 then put them in another matrix? The actual matrices am dealing are much much larger than what have given above there. Basically I want to extraxt values from column 2 corresponding to specific values in column 1 for larger matrix.

採用された回答

Jiri Hajek
Jiri Hajek 2023 年 1 月 20 日
Hi, for this, se use logical indices in MATLAB:
A=[1 1 1 1 2 2 2 2 3 3 3 3]'; B=[3 4 6 7 8 9 0 12 4 5 6 8]'; C=[A B];
selectedRowsLogicalIndices = A == 1;
subsetBdata = B(selectedRowsLogicalIndices)
subsetBdata = 4×1
3 4 6 7
  2 件のコメント
okoth ochola
okoth ochola 2023 年 1 月 20 日
Thank you, one more question kindly. Suppose matrix A was consisting of date strings from jauary to Dc say A=[01/01/2020 02/01/2020 03/01/2020 ... december], now i want to pick values that corresponds to only specific month say january. I was thinking I can convert all the dates to months only and then use the criterion above. Do you have any ideas kindly.
Jiri Hajek
Jiri Hajek 2023 年 1 月 20 日
You can use all logical operators in the expression, where you define logical indices. Also, note that dates (datetime class) can be compared using <, > operators, so it should be straightforward. So there is no need to convert dates to a different class (e.g. double).

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by