dimanche 18 mars 2018

Octave Code Generating random number list

I currently started to work with Octave at my faculty. So it's a great experience and I like this kind of "codding". I had an exercise »to create a list of at least 100 numbers between 0 and 100. »»Then to sort the list, calculate the mean for the numbers of the list, »»and finally to identify the index and number of the closest one to the mean value.

I was watching on this forum\site so I can learn more...and I will be very thankful with one feedback for the code. If it's good (beginner) or what is the improvements. Also my question what will be the difference between using "randi" function rather than "rand". I tried both and seems that this one is much usable.

And I did this:

clc;
n=input('number of values(at least 100):');
if n<100
 do
n=input('number of values(at least 100):');
until n>=100
end
N=(0+(100-0)*rand(n,1));
%ordonate the list of N 
val=input('Ordonate the list(ascendent=1,descendent=2):');
if val==1
  sort(N,'ascend');
 else 
  sort(N,'descend');
end
%calculate the mean of the values in N
disp('arithmetic=1\geometrical=2\harmonic=3') 
calc=input('Choose the mean type:');
if calc==1
  M=mean(N,'a')
 elseif calc==2
  M=mean(N,'g')
 else
  M=mean(N,'h')
end
disp('Please Wait. Searching for the closest value to the mean value:')
pause(1.5)
[c index]=min(abs(N-M));
closestValue=N(index)

THANKS !




Aucun commentaire:

Enregistrer un commentaire