How can i create a new vector with conditions?

1 回表示 (過去 30 日間)
Alex Yande
Alex Yande 2019 年 4 月 11 日
コメント済み: madhan ravi 2019 年 4 月 11 日
d=[100 200 300;
250 350 450;
810 550 600;
500 700 400]
v=[1 1 1 1]
d has 4 rows and v has 4 elements.
If d's all of the elements of any row are less than 800, v (row) must be equal to 5. But this process must continue until any element of any row is greater than 800.
Example:
d(1,:)<800 so v(1)=5
d(2,:)<800 so v(2)=5
d(3,:), all of them not less than 800. So v(3)=1
d(4,:)<800. But v(4) will not be changed. Because we find a value which is greater than 800 at row 3.
How can i achieve this problem?
Thanks,
  3 件のコメント
Stephen23
Stephen23 2019 年 4 月 11 日
"v must be have 5 elements"
I count six elements in your example output:
"Final matrix should be [5 5 5 1 1 1]."
Please revise and correct your question so that we know what you want.
Alex Yande
Alex Yande 2019 年 4 月 11 日
Question has been revised.

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

採用された回答

Stephen23
Stephen23 2019 年 4 月 11 日
>> d = [100,200,300;250,350,450;810,550,600;500,700,400]
d =
100 200 300
250 350 450
810 550 600
500 700 400
>> v = 1+4*cumprod(all(d<800,2))
v =
5
5
1
1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by