Logical comparison (>= to be specific) between each element (row) of a column vector, and all elements inside each row of a matrix having same number or rows, respectively

8 ビュー (過去 30 日間)
I am wondering if there is a better/faster way to do this, perhaps without a for loop?
Say, I have a 30x50 matrix A, and a 30x1 column vector B. I want to procuce a 30x50 logical matrix C, where each element of C is 1 if the corresponding element of A is greater than or equal to the element of B located on the same row as that element.
For a small example:
>> A = [1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18];
B = [4 10 16]';
%numbers are in ascending order to simplify example
C = false(3,6);
c = 1;
for k = B'
C(c,:) = (A(c,:) >= k);
c = c + 1;
end
The output:
C
C =
3×6 logical array
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1

採用された回答

Stephen23
Stephen23 2020 年 11 月 25 日
編集済み: Stephen23 2020 年 11 月 25 日
A = [1,2,3,4,5,6;7,8,9,10,11,12;13,14,15,16,17,18]
A = 3×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
B = [4;10;16]
B = 3×1
4 10 16
C = A>=B % requires >=R2016b
C = 3x6 logical array
0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1
C = bsxfun(@ge,A,B) % for earlier versions
C = 3x6 logical array
0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1
  1 件のコメント
Jonathan Morcos
Jonathan Morcos 2020 年 11 月 25 日
Wow, I feel quite silly. I don't know why I didn't think that would work. Thank You!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by