Finding min and max of an element without using builtin function
16 次查看(过去 30 天)
显示 更早的评论
UPDATE:
Thank you for your answers, i will share code as soon as possible.
Kindly
0 个评论
回答(3 个)
John D'Errico
2020-11-30
You are missing the point here. This is an exercise. The intent is for you to find a solution that does not use max or min.
Does sort serve the purpose? (Yes, quite well.)
Does sort do more work than max or min would perform? (Yes.)
Do you care? (No.)
Your goal was not to find some solution that you think to me optimally elegant, was it? The elegant solution is to use max and min, that is, to use the code provided which is maximally on-target. Suppose instead, you found some tool that took an array and directly returns the min and max of the vector? Almost certainly, that tool will just use min and max internally.
Sort is a good solution, and you are overthinking the problem.
4 个评论
John D'Errico
2020-11-30
编辑:John D'Errico
2020-11-30
As Jan said, just try it. Make up some data, Then see how sort works. This is how you will learn.
V = rand(1,5)
[sortedV,tags] = sort(V)
Now, what does sortedV(1) and sortedV(end) give you? Remember, that sort performs a sort in increasing order.
To find the index of those elements, what would tags(1) and tags(end) tell you?
To learn MATLAB, get your hands dirty. Get into the sandbox and play around. You seem reluctant to do that, wanting to know the optimal answer before you try anything. Computing is not that way. An answer is all you ever need, as long as the answer can be obtained as quickly as you need it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!