Extract numbers from matrix based on logical array

8 ビュー (過去 30 日間)
RE
RE 2023 年 4 月 13 日
回答済み: dpb 2023 年 4 月 14 日
I want to extract the numbers from a matrix 'x' using a logical array 'y':
x =
0.5853 0.2551 0.8909
0.2238 0.5060 0.9593
0.7513 0.6991 0.5472
y =
0 1 1
1 1 0
1 0 1
I want the solution to be in a 3x2 matrix, so something like this: z = [0.2551 0.8909; 0.2238 0.5060; 0.7513 0.5472].
If possible, no for loops should be used. I tried z=x(y==1), but that puts out a 6x1 matrix

採用された回答

dpb
dpb 2023 年 4 月 14 日
x =[
0.5853 0.2551 0.8909
0.2238 0.5060 0.9593
0.7513 0.6991 0.5472].';
y =logical([
0 1 1
1 1 0
1 0 1]).';
N=sum(y);
assert(all(N==N(1)),'Unequal numbers selected')
z=reshape(x(y),N(1),[]).'
z = 3×2
0.2551 0.8909 0.2238 0.5060 0.7513 0.5472

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by