how to find out wether the array is Hill or not?
Afficher commentaires plus anciens
Hey,
If I have an array of numbers for example
a=[1,2,2,3,3,4,4,2,-1,-1]
this array has two series: one is an Ascending series of numbers[1,2,2,3,3] , two is a descending series of numbers [4,4,2,-1,-1] an array which contains this two series is caled a Hill.
note: it still can be a descending or ascendings if the numbers in the array are equal
so how can I find out if it a Hill
I tried to use the "For" loops but matlab said that the matrix deminisions are not okay I did mot understand why?
any help please?
thank you,
1 commentaire
Walter Roberson
le 1 Jan 2019
hint: imregionalmax()
Réponse acceptée
Plus de réponses (1)
Elijah Smith
le 1 Jan 2019
This is just the first thing that I thought of:
function res = Is_hill(array)
%define variables
res = -1;
stopVar = 0;
%for the given array
for j = 1:(length(array) - 1)
%is the array rising:
if array(j) <= array(j + 1)
%if it is, keep going
continue;
else
%once it stops rising note the element it stopped at:
stopVar = j;
%exit the loop:
break;
end
end
%if it did increase and is now decreasing:
if stopVar > 1
%make sure it doesn't increase again (causing a wave not a hill):
for ii = stopVar:(length(array)-1)
%if it is decreasing
if array(ii) >= array(ii + 1)
%then go check next set of elements:
continue;
else
%if it starts to increase again then it is not a hill
res = false;
break;
end
end
else
%if it never increase or never decrease then it is not a hill:
res = false;
end
%if it's not false it must be true:
if res ~= false
res = true;
elseif res == -1
%if res hasn't changed, there was an error:
error("there was an error")
end
end
You might want to change it to fit your needs.
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!