extracting values above 0 in an array and putting them in a new array

12 ビュー (過去 30 日間)
Hatim Altarteer
Hatim Altarteer 2020 年 7 月 3 日
コメント済み: Walter Roberson 2020 年 7 月 3 日
how can i get a 1x5 array by taking values of non-zero? (e.g. taking the first column and taking the first minimum value greater than zero)?
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 3 日
Question: for column 4, should the output be 1.6807 as the "first" mininum value that is greater than 0? Or should it be 1.6054 because that is the minimum non-zero value in the row? Or should it be 3.8141 because that is the first non-zero value after the 0 ?

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

採用された回答

dipak nigam
dipak nigam 2020 年 7 月 3 日
Hey, you can use the below piece of code for your desired functionality.
a(a<=0)=inf;
minArray = min(a);
If you want to keep the original array unchanged first assign it to a temporary variable and use it to extract the result.
  2 件のコメント
Hatim Altarteer
Hatim Altarteer 2020 年 7 月 3 日
this returns a 1x5 array of ZEROs, i want NON-ZEROS minimum value.
Walter Roberson
Walter Roberson 2020 年 7 月 3 日
You are incorrect, Hatim
data = [
0 0.0754 0.2159 1.6807 2.6856
0.0754 0 0.2911 1.6054 2.7275
0.2159 0.2911 0 1.8965 2.5913
1.6807 1.6054 1.8964 0 3.8141
2.6856 2.7275 2.5913 3.8141 0
];
a = data;
a(a<=0) = inf;
minArray = min(a);
disp(minArray)
0.0754 0.0754 0.2159 1.6054 2.5913
No zeros returned at all.
dipak's solution is correct.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 7 月 3 日
matrix(matrix <= 0) = inf;
Wanted = min(matrix)
  6 件のコメント
Hatim Altarteer
Hatim Altarteer 2020 年 7 月 3 日
編集済み: Hatim Altarteer 2020 年 7 月 3 日
thanks, it was just a syntax eroor. my bad (Y)
madhan ravi
madhan ravi 2020 年 7 月 3 日
Hatim you need to accept dipak’s answer because he was first before me. I was typing the answer in my phone so didn’t realise there was an answer already.

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

カテゴリ

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

タグ

タグが未入力です。

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by