Finding min and max of an element without using builtin function

조회 수: 14 (최근 30일)
ahmed yacine LARDJANE
ahmed yacine LARDJANE 2020년 11월 30일
편집: ahmed yacine LARDJANE 2023년 6월 16일
UPDATE:
Thank you for your answers, i will share code as soon as possible.
Kindly

답변 (3개)

John D'Errico
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
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)
V = 1×5
0.9795 0.9945 0.6675 0.1575 0.9903
[sortedV,tags] = sort(V)
sortedV = 1×5
0.1575 0.6675 0.9795 0.9903 0.9945
tags = 1×5
4 3 1 5 2
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.
ahmed yacine LARDJANE
ahmed yacine LARDJANE 2020년 12월 4일
hello,
this is what i did so far
a=[15;32;5;6];
for i=1:size(a)
if a(i+1) >= a(i)
casemin=(i);
valeurmin= a(i);
end
if a(i+1) <= a(i)
casemax =(i);
valeurmax = a(i);
end
end
where case and valeur means, index and value, the code works i see the value in the workspace, but with a problem " Index exceeds matrix dimensions " in commande window, can someone help me with this? i'm really new to programming and matlab.
thanks for your help and your time

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2020년 11월 30일
  댓글 수: 1
ahmed yacine LARDJANE
ahmed yacine LARDJANE 2020년 11월 30일
thank you for your help but this one wasn't really what i'm looking for :(

댓글을 달려면 로그인하십시오.


Kirankumar Bacchewar
Kirankumar Bacchewar 2021년 10월 15일

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by