2D logical indexing of 1D array for resulting 2D array

7 ビュー (過去 30 日間)
Alex
Alex 2021 年 1 月 22 日
編集済み: Jan 2021 年 1 月 22 日
I am trying to extract the values of a 1D cell array based on a 2D logical array. Something like this:
A = {'A';
'B';
'C'};
ind = logical([1, 1; 0 0; 0 1]);
B = {'A', 'A';
[], [];
[], 'C'};
Unfortunately it's not as simple as
B = A(ind)
but I can't think of a way to do it without a cumbersome loop.

採用された回答

Jan
Jan 2021 年 1 月 22 日
編集済み: Jan 2021 年 1 月 22 日
A = {'A';
'B';
'C'};
ind = logical([1, 1; 0 0; 0 1]);
indD = double(ind) .* (1:numel(A)).'; % Auto-expand, >= R2016b
B = cell(size(ind));
B(ind) = A(indD(ind))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by