how to gain minimum value

2 次查看(过去 30 天)
Hamid
Hamid 2014-12-11
评论: Guillaume 2014-12-13
Hi everybody,
r=[1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2]
L/200<r
for example L=246 ,
I want minimum r regarding L/200<r
What I should do??
Thanks.

采纳的回答

Mischa Kim
Mischa Kim 2014-12-11
编辑:Mischa Kim 2014-12-11
Hamid you could use
r_min = r(min(find(r*200>246==1)))
r_min =
1.3000
  4 个评论
Mischa Kim
Mischa Kim 2014-12-13
Guillaume, thanks for your interesting contribution.
Are there more efficient ways of solving the problem? Definitely (see your two suggestions). Are there approaches that are didactically more effective? Possibly. I do believe that my solution provides better readability.
As far as correctness of my solution is concerned your post is somewhat inconsistent in itself, and for this reason disputable. If “It's only because the elements are ordered that it also happens to be the minimum of the values”, it seems to me that you are inferring that the solution provides exactly what the requirements are asking for. Hamid had been asking for a robust way of solving a similar but different problem (e.g. an unordered list of numbers) I would have recommended a different approach.
In summary, and in the spirit of fostering a pleasant, productive forum atmosphere I recommend that you are more careful with your wording.
Guillaume
Guillaume 2014-12-13
Hello Mischa,
We all sometimes give wrong answers. I've done so recently, I just admit it and move on.
I must say I was very surprised by your answer. I am standing by my words though, it is very wrong.
First, whenever a student writes a>b==1, it raises alarm bells. Has operator precedence been understood? Did he mean (a>b)==1 or a>(b==1)? So, it's not a good idea to give this sort of constructs to matlab beginners. In any case, in terms of readability, a>b is certainly more readable than a>b==1. For that matter,
r_filtered = r(r*200>246);
filtered_first = r_filtered(1);
would be a lot more readable and more importantly express what you're doing better.
Secondly, from an algorithm point of view min(find(x)) is also wrong. The find has to go over all the element of x to return the indices of the non-zeros, and the min then has to go over all these indices to ultimately return the first one. find(x, 1) only has to go over x until the first non-zero element. This is obviously a lot more efficient.
More importantly, your answer doesn't answer the question asked: "I want minimum r". It never looks for a minimum in r, so I'm afraid it also wrong conceptually.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2014-12-12
编辑:Guillaume 2014-12-12
For reference, (see comment to Misha's answer), the correct answer should have been:
r=[1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2];
L = 246;
r_min = min(r(r*200 > L))

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by