Replacing min and max values

3 ビュー (過去 30 日間)
Harel Harel Shattenstein
Harel Harel Shattenstein 2018 年 3 月 25 日
回答済み: Star Strider 2018 年 3 月 25 日
For a given matrix I need to replace each element which is not min or max with the value 99
For example let
M=[5 0 2;8 3 1;1 8 0]
I tried the following code
M(M~=min(M(:))|M~=max(M(:)))=99;
but it replace all the matrix elements include 8 and 0 which are the max/min elements

回答 (1 件)

Star Strider
Star Strider 2018 年 3 月 25 日
Use ‘logical indexing’:
M=[5 0 2;8 3 1;1 8 0];
Mn = M == min(M)
Mx = M == max(M)
See the documentation on Using Logicals in Array Indexing (link).
I leave the rest to you.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by