Creating logical arrays based on condition

I have an output array a[0.5,0.7,0.9], a threshold array b[0.3, 0.8, 0.7], and a lookup values array c[0,1,2]
I want a finalOutput array, f, which contains the corresponding values c if a>b else -1.
I decided to use a logical indexing array using the following code for this:
a(a>b)=1;
a(a<=b)=0;
a=logical(a);
f=c(a);
But f only contains the values of c where a=1. How can I write conditional statements to get f=[0,-1,2] ?
I also want to get the logical a in one line, because the second line of code gives incorrect outputs if I change a to [1.5, 0.7, 0.9] and b to [1.3, 0.8, 0.7].
Appreciate all suggestions.

1 件のコメント

Walter Roberson
Walter Roberson 2018 年 12 月 30 日
a(a>b)=1;
a(a<=b)=0;
a=logical(a);
could be replaced with
a = a > b;

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

 採用された回答

madhan ravi
madhan ravi 2018 年 12 月 30 日

0 投票

c(a<b)=-1
f=c

2 件のコメント

Tooba
Tooba 2018 年 12 月 30 日
c(a<b)=-1 will only replace the values of a which are less than b. What about the values of a which are greater than b?
madhan ravi
madhan ravi 2018 年 12 月 30 日
c(a>b)=.... your desired value

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

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 12 月 30 日

0 投票

>> a = [0.5,0.7,0.9];
>> b = [0.3,0.8,0.7];
>> c = [0,1,2];
>> f = c;
>> f(a<=b) = -1
f =
0 -1 2

カテゴリ

ヘルプ センター および File ExchangeData Types についてさらに検索

質問済み:

2018 年 12 月 30 日

回答済み:

2018 年 12 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by