The problem with the function 'all'

6 ビュー (過去 30 日間)
Lulu
Lulu 2011 年 11 月 18 日
Hello,
I need to compare two data sets, i.e.
a = [6 11 9; 5 4 3]; b = [1 2 3];
all(a>b); % The problem is that 'a' contains multiple rows, while 'b' - only a single row.
Therefore, the is an error: Error using > Matrix dimensions must agree.
But I'm expecting the following:
ans =
1
1
How can solve this?

採用された回答

Sven
Sven 2011 年 11 月 18 日
I think you want to compare a(1,:) to b, and a(2,:) to b. So try this using bsxfun:
all(bsxfun(@gt, a, b),2)
Does that give what you expected?

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 11 月 18 日
all(a > repmat(b,size(a,1),1),2)

Jan
Jan 2011 年 11 月 18 日
All values are smaller, if the smallest is smaller:
a = [6 11 9; 5 4 3];
b = [1 2 3];
min(a, [], 1) > b
For a=rand(1000, 1000), b = rand(1, 1000) this 30% faster than the BSXFUN approach, I assume because it needs less temporary memory.
  1 件のコメント
Sven
Sven 2011 年 11 月 18 日
I was thinking something like this might be more efficient. I think the answer that mimics:
all(bsxfun(@gt, a, b),2)
would if fact be:
min(a,[],2) > max(b)

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

カテゴリ

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