Effacer les filtres
Effacer les filtres

how to find out wether the array is Hill or not?

2 vues (au cours des 30 derniers jours)
aya qassim
aya qassim le 1 Jan 2019
Commenté : aya qassim le 1 Jan 2019
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,

Réponse acceptée

the cyclist
the cyclist le 1 Jan 2019
Modifié(e) : the cyclist le 1 Jan 2019
I think this is right, but I have to admit I have not thoroughly checked it:
% Original data
a=[1,2,2,3,3,4,4,2,-1,-1];
% Find difference between consecutive elements
s = diff(a);
% Find the last "uphill" element, and the first "downhill" element
u = find(s>0,1,'last');
d = find(s<0,1,'first');
% Make sure the last uphill element occurs before the first downhill element
is_hill = u<d
The variable is_hill will be true if a is a "hill", and false if not.

Plus de réponses (1)

Elijah Smith
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 Mathematics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by