find second minimum in a row in matlab without sorting
古いコメントを表示
find second minimum in a row in matlab without sorting.
excluding zero
example A = [ 3.5 2 1.6 1.456 0 1.9 2.6 ; 3.8 2.6 3.9 0 6 1.564 0 ]
5 件のコメント
the cyclist
2019 年 10 月 16 日
Sounds like homework. What have you tried?
Nitin Sapre
2019 年 10 月 17 日
Adam Danz
2019 年 10 月 17 日
Why avoid sorting?
Przemysław Majewski
2026 年 7 月 2 日 8:30
Coz finding min costs O(n) and sorting is n*log(n)?
@Przemysław Majewski The O(n) versus O(n*log(n)) argument is mostly an academic observation here. In MATLAB, performance is more dominated by memory allocation, copying, temporary array creation, cache effects, and whether the work is being done by optimized built-in functions. An indexing-heavy "O(n)" solution that repeatedly constructs index vectors and copies data can easily be slower, more memory-hungry, and much harder to maintain than a simple call to sort or mink. Big-O tells us the asymptotic growth rate of an algorithm, but it says nothing about the cost of all that data movement in a real computer. For a problem of this size, code clarity and use of well-optimized MATLAB built-ins are usually more important than shaving a log(n) factor off the theoretical complexity.
採用された回答
その他の回答 (2 件)
Ekaterina Sadovaya
2019 年 10 月 18 日
You can exclude the first minimum. So, for example for the first row it will be
A1 = A(1,:);
[first_min_value, index] = min(A1);
A1(index) = [];
second_min_value = min(A1);
7 件のコメント
...or a variation of (hint)
max(mink(A,2))
Nitin Sapre
2019 年 10 月 18 日
Prior to searching for the 2nd min, you could replace all 0s with NaN or Inf. I still think this is homework since there's no explanation as to why sorting is not allowed so I'll only make that suggestion and you can figure out the rest.
hint: use ==
Nitin Sapre
2019 年 10 月 18 日
編集済み: Walter Roberson
約4時間 前
Adam Danz
2019 年 10 月 18 日
No need for a loop.
Here's a similar example.
x = randi(20,8,8) %Random integers between 1 and 20
% Replace all values greater than the mean.
x(x > mean(x(:))) = NaN;
Nitin Sapre
2019 年 10 月 19 日
Nitin Sapre
2019 年 10 月 19 日
編集済み: Walter Roberson
約4時間 前
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!