choosing values with criteria from a set of values given

1 回表示 (過去 30 日間)
ker fang fang
ker fang fang 2015 年 1 月 12 日
回答済み: Guillaume 2015 年 1 月 12 日
hi, i need some help here. can someone teach me how to do the following?
for example, i have a set of values a = [ 1 5 6 10 15], then i have another set of values b = [ 1 0.8 0 -0.8 -1.8] , another set c = [3 2.4 0 -2.4 -5.4].
how do i do when i want the answer to be 'a' if the values in c is greater than a or 'b' if the values in c is less than a?
in this example the expected ans should be 'd' [ 1 0.8 0 -0.8 -1.8] ? how do i make that?

回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 1 月 12 日
out=a
out(c<a)=b(c<a)

Stalin Samuel
Stalin Samuel 2015 年 1 月 12 日
a = [ 1 5 6 10 15], b = [ 1 0.8 0 -0.8 -1.8] c = [3 2.4 0 -2.4 -5.4]
a1=sum(a) b1= sum(b) c1=sum(c) if c1>b1 result=a elseif c1<a1 result=b end
  1 件のコメント
ker fang fang
ker fang fang 2015 年 1 月 12 日
hi, but i want to display the values, instead of adding them up.

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


Guillaume
Guillaume 2015 年 1 月 12 日
Use logical indexing. The expression c>a returns a logical array the same size as a and c with 1 where c is greater than a and 0 otherwise.
If you use this logical array to index into another array (e.g. a), then you only get those value of this other array for which the logical array is 1. Hence
a(c>a)
returns only those values of a where c is greater than a.
One possible solution is thus:
d = b
d(c>a) = a(c>a);
Note that you haven't said what should happen when a is equal to c, the above code assumes you want b.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by