Pairs of elements that meet a condition?

2 ビュー (過去 30 日間)
Marco Bakker
Marco Bakker 2016 年 10 月 9 日
編集済み: Massimo Zanetti 2016 年 10 月 9 日
Suppose all_val is an nx2 matrix of real numbers.
How to get a matrix cond_val with only the rows of all_val for which
fun(j1)*fun(j2) < 0 % for some scalar-input function fun.m
Where j1 is the element from column 1 and j2 is the corrosponding element from column 2 of the rows of all_val.
I've had a look at Find Array Elements That Meet a Condition but have had no luck.

採用された回答

Guillaume
Guillaume 2016 年 10 月 9 日
編集済み: Guillaume 2016 年 10 月 9 日
If fun can only operate on scalar:
cond_val = all_val(prod(arrayfun(@fun, all_val), 2) < 0, :);
If fun can operate on whole arrays at once, then simply:
cond_val = all_val(prod(fun(all_val), 2) < 0, :);

その他の回答 (2 件)

Massimo Zanetti
Massimo Zanetti 2016 年 10 月 9 日
編集済み: Massimo Zanetti 2016 年 10 月 9 日
A=rand(15,2);
f=@(x) x.^2; %or another function you define
C=cell2mat( arrayfun( @(k) f(A(k,1))*f(A(k,2))<0 , (1:15)' ) );

Star Strider
Star Strider 2016 年 10 月 9 日
I have no idea what ‘fun’ does or what ‘all_vals’ contains, but you can do something like this to create a logical vector output:
all_vals = randi(9, 10, 2); % Create Matrix
fun = @(x) x - 5; % Create Function
Out = (fun(all_vals(:,1)) .* fun(all_vals(:,2))) < 0; % Logical Vector Output
Elements of ‘Out’ equal to 1 are true so the condition was met, 0 are false.

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by