How to limit a vector to a certain maximum

I have the vector x = [20 19 23 24 17] and I want to somehow create a vector that contains nothing greater than 20. That is, I need a vector that returns y = [20 19 20 20 17] from the original vector. The methods I have tried only return some sort of a boolean type vector that tells me which values are greater than 20. Can anyone help me?

1 件のコメント

Bill Tubbs
Bill Tubbs 2021 年 5 月 6 日
See this answer which provides a simple function to do this.

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

回答 (1 件)

Image Analyst
Image Analyst 2012 年 11 月 29 日

8 投票

xMax = 20;
x = [20 19 23 24 17]
% Clip to xMax
x(x>xMax) = xMax

5 件のコメント

Phil
Phil 2012 年 11 月 29 日
Is there a way to create an entirely new vector with the ceiling of 20 instead of just replacing the old one?
Walter Roberson
Walter Roberson 2012 年 11 月 29 日
newvector = min(x, 20);
Reno Marino
Reno Marino 2020 年 2 月 13 日
u da man
James Pieris
James Pieris 2020 年 9 月 5 日
Hi, do you think you can reference the matlab feature x (x> xmax) from which you derived this technique? I am just interested to know where this defined/listed in matlab documentation. Thanks a lot. it works.
Image Analyst
Image Analyst 2020 年 9 月 6 日
Do a search in the help for "logical indexing".

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2012 年 11 月 29 日

コメント済み:

2021 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by