Logical indexing returns different dimensions

a = [3, 2]
a = 1×2
3 2
b = [1; 1] == 1
b = 2×1 logical array
1 1
a(b) % returns a 1x2 array
ans = 1×2
3 2
a = [3, 2; 4, 5]
a = 2×2
3 2 4 5
b = [1; 1; 1; 1] == 1
b = 4×1 logical array
1 1 1 1
a(b) % returns a 4x1 array
ans = 4×1
3 4 2 5

1 件のコメント

Umesh Kumar Singla
Umesh Kumar Singla 2023 年 1 月 12 日
編集済み: Umesh Kumar Singla 2023 年 1 月 12 日
I was expecting both to return 1 x n type arrays but that's not the case. Am I missing something?

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

 採用された回答

Matt J
Matt J 2023 年 1 月 12 日
編集済み: Matt J 2023 年 1 月 12 日

0 投票

Vectors have different indexing behavior than matrices. There's nothing more to it, except perhaps to point out that it's not just logical indexing. It applies to regular indexing as well:
a=[10,20,30,40];
a([1;2])
ans = 1×2
10 20
a=[10,20;30 40];
a([1;2;3;4])
ans = 4×1
10 30 20 40

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 1 月 12 日

編集済み:

2023 年 1 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by